"Stephen McInerney" <[EMAIL PROTECTED]> wrote 

> I was asking if it's a recognized good programming practice to
> declare and initialize *all* members in the class defn.
> I think I'm hearing a general yes on that - any other opinions?

It depends on what you mean by the class definition.

In general attributes are defined in the classes methods, 
and since they are part of the class definition then I'd say 
yes its general practice.

I'd go further and say its bad practice to define class/instance 
attributes outside the class unless you have a good reason to 
do so - such as dynamically creating classes at run time.

Thus:

class foo:
    def __init__(self, x, y):
        self.x = x
        self.y = y

f = foo(22,66)

Is preferable to

class foo: pass

f = foo()
f.x = 22
f.y = 66

But there are occasions when adding attributes dynamically
can be a sensible approach - your own example of parsing XML 
is a good example.

But because there are always exceptions you won't see many 
people making hard and fast rules about such things in Python.

Alan G

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to