empty classes as c structs?

Steven Bethard steven.bethard at gmail.com
Mon Feb 7 12:06:37 EST 2005


Nick Coghlan wrote:
> Finally, I've just used normal names for the functions. I think the 
> issue of function shadowing is best handled by recommending that all of 
> the functions be called using the class explicitly - this works just as 
> well for instance methods as it does for class or static methods.

I wonder if it would be worth adding a descriptor that gives a warning 
for usage from instances, e.g.:

py> import new
py> import warnings
py> class InstanceWarner(object):
...     def __init__(self, func):
...         self.func = func
...     def __get__(self, obj, type=None):
...         if obj is None:
...             return self.func
...         else:
...             warnings.warn('methods of this type should not be '
...                           'invoked from instances')
...             return new.instancemethod(self.func, obj, type)
...
py> class Bunch(object):
...     @InstanceWarner
...     def update(self):
...         print 'updating', self
...
py> Bunch.update(Bunch())
updating <__main__.Bunch object at 0x01152830>
py> Bunch().update()
__main__:8: UserWarning: methods of this type should not be invoked from 
instances
updating <__main__.Bunch object at 0x011527D0>

Steve



More information about the Python-list mailing list