object instance after if isalpha()

Ben Cartwright bencvt at gmail.com
Wed Apr 12 20:42:44 EDT 2006


Marcelo Urbano Lima wrote:
> class abc:
>   def __init__(self):
>     name='marcelo'
> print x.name
> Traceback (most recent call last):
>   File "1.py", line 12, in ?
>     print x.name
> AttributeError: abc instance has no attribute 'name'

In Python, you explicitly include a reference to an object when setting
or accessing the object's attributes... even when you're inside one of
that objects methods.  I.e.:

  class abc:
    def __init__(self):
      self.name='marcelo'

When you omit the "self." bit, Python creates a variable local to
__init__() named "name", and the attribute is never set.  This is
different from some other OO languages (e.g. C++/Java/C#'s "this"), may
take some getting used to.

Hope that helps,
--Ben




More information about the Python-list mailing list