What’s the simplest way to access a classes namespace from withinName or namespace? You can access the class name from an instance via
itself. I want to use it in a custom __repr__() method so it prints the
current namespace for the class like package.module.class.
the __class__ attribute:
.... def __repr__(self):>>class foo:
.... return "<%s instance at %x>" % (
.... self.__class__.__name__, id(self)
.... )
....
<foo instance at c714e0>>>foo()
The module the class was defined in is also available:
.... def __repr__(self):>>class foo:
.... return "<%s.%s instance at %x>" % (
.... self.__class__.__module__, self.__class__.__name__,
.... id(self)
.... )
....
<__main__.foo instance at c9fbc0>>>foo()
To get the namespace (that is, the collection of (name, value)
pairs that make up the class' contents), use vars(self.__class__)
(or vars(self), if you want the full instance).
</F>