Accessing class-level variables

Philip Swartzleonard starx at pacbell.net
Wed Apr 24 00:27:51 EDT 2002


Gerhard =?iso-8859-15?Q?H=E4ring?= || Tue 23 Apr 2002 09:04:04p:

> I'm trying to access a class variable in a derived class. I sincerly
> hope there's a less kludgy way than what I'm currently using:
> 
> class X:
>     a = 5
> 
> class Y(X):
>     def p(self):
>         print self.__class__.a
> 
> Y().p()
> 
> Gerhard

Well, i knew this worked for dispatching __init__'s, but... well here 
=):

Python 2.2.1 (#34, Apr  9 2002, 19:34:33) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
IDLE 0.8 -- press F1 for help
>>> class A:
	d = 5

>>> class B:
	def p(self):
		print A.d

>>> z = B()
>>> z.p()
5
-- 
Philip Sw "Starweaver" [rasx] :: www.rubydragon.com



More information about the Python-list mailing list