I was surprised that I can use __class__
unqualified inside methods!
*Given these facts:
1) __class__
is not defined in either global or builtin scopes of the interactive session
2) Tested under Python 3.3.3 and Python 2.7.11
Example:
class Z:
def __init__(self):
print(__class__)
Works under Python 3.3.3, but when tested with Python 2.7.11, this raises an error with classic and new-style classes. As usual when we want to use a variable defined inside a class we have to use obj.attr
. surprisingly for __class__
it just works!
class Z(object):
a = 20
def __init__(self):
print(__class__)
print(a) # doesn't work
Where does __class__
come from?
Is this a bug?