Does Python have Class methods

Costas Menico costas at meezon.com
Fri May 11 06:44:08 EDT 2001


David Bolen <db3l at fitlinxx.com> wrote:

>costas at meezon.com (Costas Menico) writes:
>
>> I guess a similar concept would apply to class variables? Although I
>> was under the impression that anything defined in a class outside of a
>> method, is considered a class variable. Of course I tried it but does
>> not work.
>
>Can you describe what you wanted to happen that didn't?  It is true
>that any definitions outside of a method exist at the class level, and
>can be accessed either directly through the class, or indirectly as an
>instance reference (e.g., Python proceeds up the inheritance tree just
>as it does for methods).  And there is only one such definition at the
>class level so all instances see the same value - although if an
>instance is using an instance reference (e.g., self.xxx) then it could
>be blocked from the class value if it has locally defined the same
>name or some superclass has.

I have a class like this:

# class stored in c:\fooclass.py
class fooClass:
	nm = 1
	def __init__(self):
		pass


# Typed in interactive window
import sys
sys.path.append('c:\\')
import fooclass
f1 = fooclass.fooClass()
f1.nm + = 1   # increment to 2

f2 = fooclass.fooClass()
f2.nm  # Still gives 1

I thought here f2.nm would be a 2

regards,

costas




More information about the Python-list mailing list