Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Can't refer to base class attribute?

Reply
Thread Tools

Can't refer to base class attribute?

 
 
mrstephengross
Guest
Posts: n/a
 
      10-23-2007
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 ===

 
Reply With Quote
 
 
 
 
Marc 'BlackJack' Rintsch
Guest
Posts: n/a
 
      10-23-2007
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Base class method Need base class value Karan Rajput Ruby 2 12-22-2010 04:47 PM
base class public type (non template and template base class) Hicham Mouline C++ 1 04-20-2009 03:28 PM
Access of base class' private base class: qualification required, why Alf P. Steinbach C++ 6 09-03-2005 04:03 PM
Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class. DJ Dev ASP .Net 3 02-08-2004 04:19 PM
Re: How does an inner class (non-static) refer to members of it's enclosing class? Tor Iver Wilhelmsen Java 0 07-18-2003 08:41 AM



Advertisments