473,386 Members | 1,793 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 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 3378
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: 5tein | last post by:
I mean, with XSL functions such as SELECT can I only refer to XML element values or can I somehow also refer to XML attribute values? Is there any way to refer to XML content based on attribute...
4
by: David | last post by:
I have trying to have a webform inherit controls from another form and can't get it to work Say I have a form that saves the person's demographic info. ****one.aspx**** //I have an object...
6
by: scottyman | last post by:
I can't make this script work properly. I've gone as far as I can with it and the rest is out of my ability. I can do some html editing but I'm lost in the Java world. The script at the bottom of...
14
by: Dave Booker | last post by:
It looks like the language is trying to prevent me from doing this sort of thing. Nevertheless, the following compiles, and I'd like to know why it doesn't work the way it should: public class...
17
by: lm401 | last post by:
I'm trying to work with the following idea: class animal: def __init__(self, weight, colour): self.weight = weight self.colour = colour class bird(animal): def __init__(self, wingspan):
3
by: forest demon | last post by:
for example, let's say I do something like, System.Diagnostics.Process.Start("notepad.exe","sample.txt"); if the user does a SaveAs (in notepad), how can i capture the path that the user...
0
by: phobos7 | last post by:
Hi, I'm looking for a way to force a derived class to serialise/serialize as its base class. I was hoping that this was possible by just applying an attribute to the the sub class. public...
1
by: chris.bahns | last post by:
Hello All, I am fairly new to creating custom attributes, and want to get a better feel for how they should be named. We have a class hierarchy, with a base class called "SequenceCommand". This...
2
by: =?Utf-8?B?aGVyYmVydA==?= | last post by:
I defined a WCF callback contract: <ServiceContract()_ Interface IEBAPEvents 'this interface defines the events to be triggered in the clients 'the EBAP pattern defines two events: a progress...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.