Check For SELF Variable Existance

Simon Brunning simon at brunningonline.net
Tue Mar 11 09:21:23 EDT 2008


On Tue, Mar 11, 2008 at 11:00 AM, Robert Rawlins
<robert.rawlins at thinkbluemedia.co.uk> wrote:

> I want to be able to check if a class has a certain property in its 'self'
> scope, what's the best way to do this?

>>> class Spam(object):
...     def egg(self):
...         if hasattr(self, 'chips'): print 'got chips!'
...
>>> spam = Spam()
>>> spam.egg()
>>> spam.chips = 'beans'
>>> spam.egg()
got chips!

-- 
Cheers,
Simon B.
simon at brunningonline.net
http://www.brunningonline.net/simon/blog/



More information about the Python-list mailing list