Can't refer to base class attribute?

M

mrstephengross

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 ===
 
M

Marc 'BlackJack' Rintsch

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top