Message199126
| Author |
ethan.furman |
| Recipients |
arigo, eli.bendersky, eric.snow, ethan.furman, ronaldoussoren |
| Date |
2013年10月07日.03:25:28 |
| SpamBayes Score |
-1.0 |
| Marked as misclassified |
Yes |
| Message-id |
<1381116329.81.0.987690490533.issue16938@psf.upfronthosting.co.za> |
| In-reply-to |
| Content |
=========================================================================
class Boom:
def __dir__(self):
return ['BOOM']
def __getattr__(self, name):
if name =='BOOM':
return 42
return super().__getattr(name)
help(Boom)
=========================================================================
Help on class Boom in module __main__:
class Boom(builtins.object)
| Methods defined here:
|
| __dir__(self)
|
| __getattr__(self, name)
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables (if defined)
|
| __weakref__
| list of weak references to the object (if defined)
(END)
=========================================================================
So on an instance there is no problem.
=========================================================================
import inspect
class MetaBoom(type):
def __dir__(cls):
return ['__class__', '__module__', '__name__', 'BOOM']
def __getattr__(self, name):
if name =='BOOM':
return 42
return super().__getattr(name)
def test(self):
print('testing...')
class Boom(metaclass=MetaBoom):
pass
help(Boom())
=========================================================================
Help on Boom in module __main__ object:
<class '__main__.Boom'>
(END)
=========================================================================
Still have problem with metaclasses.
Looking at the result from inspect.classify_class_attrs() we see:
=========================================================================
Attribute(name='BOOM', kind='data', defining_class=None,
object=<object object at 0x7f061f04d200>)
=========================================================================
The defining class for BOOM should be Boom, not None.
With the attached patch, we get:
=========================================================================
Help on Boom in module __main__ object:
class Boom(builtins.object)
| Data and other attributes defined here:
|
| BOOM = 42
(END)
========================================================================= |
|