471,330 Members | 1,783 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,330 software developers and data experts.

Can't refer to base class attribute?

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

Oct 23 '07 #1
1 3297
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
Oct 23 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by scottyman | last post: by
17 posts views Thread by lm401 | last post: by
1 post views Thread by chris.bahns | last post: by
2 posts views Thread by =?Utf-8?B?aGVyYmVydA==?= | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.