Question about accessing class-attributes.

Duncan Booth duncan at NOSPAMrcp.co.uk
Thu Apr 24 08:25:10 EDT 2003


Nikolai Kirsebom <nikolai.kirsebom.NOJUNK at siemens.no> wrote in 
news:0kifav8dfcbu0t8j4qrrkq6ag7gcg1s062 at 4ax.com:

> class A(object):
>      InstCount = 0
>      def __init__(self):
>           self.__class__.__dict__['InstCount'] += 1
> 
> class B(A):
>      InstCount = 0
> 
> class C(A):
>      InstCount = 0
> 
> Is the syntax used for incrementing the class-attribute for every
> instanciation 'the way' it should be done ?

It would be more normal just to write:

		self.__class__.InstCount += 1

Alternatively you could create a class method to do the increment, but that 
is probably needlessly complex here.

-- 
Duncan Booth                                             duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?




More information about the Python-list mailing list