Python class variable name vs __name__ -
i'm trying understand relationship between variable python class object assigned , __name__
attribute class object. example:
in [1]: class foo(object): ...: pass ...: in [2]: foo.__name__ = 'bar' in [3]: foo.__name__ out[3]: 'bar' in [4]: foo out[4]: __main__.bar in [5]: bar --------------------------------------------------------------------------- nameerror traceback (most recent call last) <ipython-input-5-962d3beb4fd6> in <module>() ----> 1 bar nameerror: name 'bar' not defined
so seems have changed __name__
attribute of class can't refer name. know bit general explain relationship between foo
, foo.__name__
?
it's simple. there no relationship @ all.
when create class local variable created name used, pointing @ class can use it.
the class gets attribute __name__
contains name of variable, because that's handy in cases, pickling.
you can set local variable else, or change __name__
variable, things pickling won't work, don't that.
Comments
Post a Comment