__class__ of what

Jean-Michel Pichavant jeanmichel at sequans.com
Thu Aug 12 16:06:05 CEST 2010


Eric J. Van der Velden wrote:
> Hello,
>
> I have,
>
> class C:
>         n=0
>         def __init__(s):
>                 __class__.n+=1
>
>   

Should be

class C:
    n = 0
    def __init__(self):
       self.__class__.n+=1
       C.n+=1 # equivalent to this line (I prefer this one, more 
readable, less refactor-friendly)

  @classmethod
  def p(cls):
    print(cls.n)

JM





More information about the Python-list mailing list