get a self data in a method call
Bengt Richter
bokr at oz.net
Mon Dec 23 10:00:51 EST 2002
On 23 Dec 2002 07:23:15 GMT, bokr at oz.net (Bengt Richter) wrote:
[... several examples, one with a bad sentinel ...]
><you can skip this>
>Well, I don't really like referring to SomeClass.anything
>from a method within SomeClass. It's an ugly dependency
>on a global binding IMO. With new classes there is probably
>a way around it. Ok, here's a go at getting around a global binding,
>in this case of a sentinel:
>
> >>> class MyClass(object):
> ... _sentinel = None
^^^^
This is a goof, sorry ;-/
_sentinel should be bound to a unique object, not None, which
is the same object for every specified reference to None ;-/
Pretty silly, having warned about using None ;-/ Sorry about that.
Just typed in None and didn't test very well. Noticed when looking
back at it now. Instead, this should be better:
>>> class MyClass(object):
... _sentinel = object()
... default = 'A class variable available via any instance'
... class __metaclass__(type):
... def __init__(cls, name, bases, dct):
... def func(self, data=cls._sentinel):
... if data is cls._sentinel: data = getattr(self,'data','<no data>')
... return (data, self.default) # or otherwise use them
... setattr(cls, 'func', func)
... def __init__(self, data=cls._sentinel):
... if data is cls._sentinel: return # don't set it if not specified
... self.data = '%s [%s]' % (data, self.default[0]) # silly mod using class data
... setattr(cls, '__init__', __init__)
... def ordinary(self): return 'ordinary, returning self.data if avail: %s' % self.data
...
>>> mc=MyClass()
>>> mc.default
'A class variable available via any instance'
>>> mc.func()
('<no data>', 'A class variable available via any instance')
>>> vars(mc)
{}
>>> mc.func('func arg')
('func arg', 'A class variable available via any instance')
>>> vars(mc)
{}
>>> mc.func(None)
(None, 'A class variable available via any instance')
>>> mc=MyClass('specified')
>>> vars(mc)
{'data': 'specified [A]'}
>>> mc.data
'specified [A]'
>>> mc.func('func arg')
('func arg', 'A class variable available via any instance')
>>> mc.func()
('specified [A]', 'A class variable available via any instance')
>>> del mc.data
>>> mc.func()
('<no data>', 'A class variable available via any instance')
>>> mc=MyClass(None)
>>> vars(mc)
{'data': 'None [A]'}
>>> mc.data
'None [A]'
>>> mc.func('func arg')
('func arg', 'A class variable available via any instance')
>>> mc.func()
('None [A]', 'A class variable available via any instance')
>>> del mc.data
>>> mc.func()
('<no data>', 'A class variable available via any instance')
>>> mc.data = 'added directly'
>>> mc.func()
('added directly', 'A class variable available via any instance')
>>> mc.func(MyClass._sentinel) # should fool it to do same as mc.Func()
('added directly', 'A class variable available via any instance')
>>> mc.func('x')
('x', 'A class variable available via any instance')
Regards,
Bengt Richter
More information about the Python-list
mailing list