__class__ of what
Peter Otten
__peter__ at web.de
Thu Aug 12 10:21:07 EDT 2010
Eric J. Van der Velden wrote:
> I have,
>
> class C:
> n=0
> def __init__(s):
> __class__.n+=1
>
>
> I do
>>>> C()
>
> This is fine. But of what thing I am taking the __class__ of?
> I can also do
>
> @staticmethod
> def p():
> print(__class__.n)
>
>>>> C.p()
> 1
I had no idea that this existed. __class__ was probably added to make
super() calls without an explicit class argument possible in Python 3. It is
made available as a closure:
>>> class A:
... def f(self): __class__
... def g(self): pass
...
>>> A.f.__closure__[0].cell_contents
<class '__main__.A'>
>>> A.g.__closure__ is None
True
Peter
More information about the Python-list
mailing list