How to 'declare' class attributes?

eloy eloy at rabinf50.uco.es
Sat May 26 11:28:40 EDT 2001


Dear snaky friends,

	I have a doubt about what we could call 'programming philosophy'.
In some other OO languages, attributes of a class must be declared. In
Python, we can, as far as I know, take different approaches:

	1.- to 'declare' (I mean, bind) all of them in the class
		definition:

			class foo:
				bar= None
				baz= 1
				fubar= None
				...

	2.- to bind them in __init__:

			class foo:
				def __init__(self):
					self.bar= ...
					self.baz= ...
					self.fubar= ...
					...

	3.- _not_ to bind them until needed:

			class foo:
				...
				def xyzzy(...):
					self.bar= ...
					self.baz= ...
				...
				def frob(...):
					...
					self.fubar= ...
					...
				...

	(yes, yes, I'm the Lord of Ellipsis...)

	In my opinion, solution 1 needs good design (I mean _previous_
design) and/or good book-keeping to maintain the list of attributes up to
date. Solution 2 seems better, and solution 3 is the worst.

	Considering the legibility of Python, and the good documentation
it allows us to create, I want to ask:

	What kind of attribute handling (let's call it 'declaration') do
you use?. Is there a 'standard' or 'best' way to keep track of the
attributes in Python classes?.

	Thanks for your time.

	(And excuse the bad puns: today is saturday and the final day of
Cordoba's May Fair, so...)

                                        -- Eloy
_____________________________________________________________________
---- Eloy Rafael Sanz Tapia -- ersanz at uco.es -- ma1satae at uco.es -----
------------------ http://www.uco.es/~ma1satae ----------------------
----------- GPG ID: 190169A0 / finger eloy at rabinf50.uco.es ----------
Córdoba _ España _______________ Debian 2.2 GNU/Linux 2.2.19 rabinf50




More information about the Python-list mailing list