Can't refer to base class attribute?

Discussion in 'Python' started by mrstephengross, Oct 23, 2007.

  1. I've got a Base class with an attribute "foo" (of type Foo), and a
    Derived class (derived from Base). In Derived's constructor, I try to
    refer to Base.foo, but python complains:
    AttributeError: class Base has no attribute 'foo'

    Any ideas? (code below)

    === CODE ===
    #!/usr/bin/python

    class Foo:
    def __init__ (self):
    self.x = 3

    class Base:
    def __init__ (self):
    self.foo = Foo()

    class Derived(Base):
    def __init__(self):
    Base.__init__(self)
    Base.foo.x = 5

    Derived()
    === EOF ===
     
    mrstephengross, Oct 23, 2007
    #1
    1. Advertising

  2. On Tue, 23 Oct 2007 18:54:06 +0000, mrstephengross wrote:

    > I've got a Base class with an attribute "foo" (of type Foo), and a
    > Derived class (derived from Base). In Derived's constructor, I try to
    > refer to Base.foo, but python complains:
    > AttributeError: class Base has no attribute 'foo'


    Because the class `Base` doesn't have an attribute `foo`. Just believe
    the error message. :)

    > Any ideas? (code below)
    >
    > === CODE ===
    > #!/usr/bin/python
    >
    > class Foo:
    > def __init__ (self):
    > self.x = 3
    >
    > class Base:
    > def __init__ (self):
    > self.foo = Foo()



    `Base` has no `foo` attribute but *instances* of `Base` have.

    > class Derived(Base):
    > def __init__(self):
    > Base.__init__(self)
    > Base.foo.x = 5


    Instances of `Derived` have a `foo` attribute inherited from `Base`. So
    the last line should be ``self.foo.x = 5``.

    Ciao,
    Marc 'BlackJack' Rintsch
     
    Marc 'BlackJack' Rintsch, Oct 23, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. 5tein
    Replies:
    1
    Views:
    473
    Patrick TJ McPhee
    Apr 30, 2004
  2. Alf P. Steinbach
    Replies:
    6
    Views:
    549
    John Carson
    Sep 3, 2005
  3. Replies:
    21
    Views:
    16,225
    Harlan Messinger
    Dec 14, 2007
  4. Hicham Mouline
    Replies:
    1
    Views:
    595
    Victor Bazarov
    Apr 20, 2009
  5. Karan Rajput
    Replies:
    2
    Views:
    145
    Abinoam Jr.
    Dec 22, 2010
Loading...

Share This Page