473,556 Members | 2,506 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

refering to base classes

hi - Im quite new to python, wondering if anyone can help me understand
something about inheritance here. In this trivial example, how could I
modify the voice method of 'dog' to call the base class 'creatures'
voice method from with in it?

class creature:
def __init__(self):
self.noise=""
def voice(self):
return "voice:" + self.noise

class dog(creature):
def __init__(self):
self.noise="bar k"

def voice(self):
print "brace your self:"

thanks
glenn

Aug 29 '06 #1
21 1745
glenn wrote:
hi - Im quite new to python, wondering if anyone can help me understand
something about inheritance here. In this trivial example, how could I
modify the voice method of 'dog' to call the base class 'creatures'
voice method from with in it?

class creature:
def __init__(self):
self.noise=""
def voice(self):
return "voice:" + self.noise

class dog(creature):
def __init__(self):
self.noise="bar k"

def voice(self):
print "brace your self:"

thanks
glenn
Try this:

class dog(creature):
.....
def voice(self):
print "brace your self:"
creature.voice( self)

This should do it.
Aug 29 '06 #2
Chaz Ginger wrote:
glenn wrote:
>hi - Im quite new to python, wondering if anyone can help me understand
something about inheritance here. In this trivial example, how could I
modify the voice method of 'dog' to call the base class 'creatures'
voice method from with in it?

class creature:
def __init__(self):
self.noise=""
def voice(self):
return "voice:" + self.noise

class dog(creature):
def __init__(self):
self.noise="bar k"

def voice(self):
print "brace your self:"

thanks
glenn
Try this:

class dog(creature):
.....
def voice(self):
print "brace your self:"
creature.voice( self)

This should do it.
I did forget to mention that in 'dog"s' __init__ you had better call
creature's __init__. You might make it look like this:

def __init__(self):
self.noise = 'bark'
creature.__init __(self)

There is another approach - using Superclass - but I will leave that
exercise to the reader.

Aug 29 '06 #3
glenn wrote:
[...] In this trivial example, how could I modify the voice method of
'dog' to call the base class 'creatures' voice method from with in it?

class creature:
def __init__(self):
self.noise=""
def voice(self):
return "voice:" + self.noise

class dog(creature):
def __init__(self):
self.noise="bar k"

def voice(self):
print "brace your self:"
If you want dog.voice() to just print "voice: bark", you just have to omit
the voice method for the dog class: it will be inherited from creature.

If you want dog.voice() to do something else, you can call superclass'
method like this:

def voice(self):
creature.voice( self)
print "brace your self"
any_other_magic ()

HTH
--
Roberto Bonvallet
Aug 29 '06 #4
glenn wrote:
hi - Im quite new to python, wondering if anyone can help me understand
something about inheritance here. In this trivial example, how could I
modify the voice method of 'dog' to call the base class 'creatures'
voice method from with in it?

class creature:
def __init__(self):
self.noise=""
def voice(self):
return "voice:" + self.noise

class dog(creature):
def __init__(self):
self.noise="bar k"

def voice(self):
print "brace your self:"

<ot>
It might be better to use newstyle classes if you can. Also, the
convention is to use CamelCase for classes names (unless you have a
strong reason to do otherwise).
</ot>

Here you could use a class attribute to provide a default:

class Creature(object ):
noise = ""

def voice(self):
return "voice:" + self.noise
class Dog(Creature):
noise="bark"

def voice(self):
print "brace your self:"
return Creature.voice( self)
# can also use this instead, cf the Fine Manual
return super(Dog, self).voice()

My 2 cents
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom. gro'.split('@')])"
Aug 29 '06 #5
Chaz Ginger wrote:
Chaz Ginger wrote:
glenn wrote:
hi - Im quite new to python, wondering if anyone can help me understand
something about inheritance here. In this trivial example, how could I
modify the voice method of 'dog' to call the base class 'creatures'
voice method from with in it?

class creature:
def __init__(self):
self.noise=""
def voice(self):
return "voice:" + self.noise

class dog(creature):
def __init__(self):
self.noise="bar k"

def voice(self):
print "brace your self:"

I did forget to mention that in 'dog"s' __init__ you had better call
creature's __init__. You might make it look like this:

def __init__(self):
self.noise = 'bark'
creature.__init __(self)
There's a problem with Chaz's __init__() method. Notice that the
creature class's __init__ sets self.noise to the empty string. In this
case, the superclass's __init__() method should be called first:

class dog(creature):
def __init__(self):
creature.__init __(self)
self.noise = "bark"
def voice(self):
print "brace your self:"
creature.voice( self)

--Jason

Aug 29 '06 #6
Jason wrote:
Chaz Ginger wrote:
>Chaz Ginger wrote:
>>glenn wrote:
hi - Im quite new to python, wondering if anyone can help me understand
something about inheritance here. In this trivial example, how could I
modify the voice method of 'dog' to call the base class 'creatures'
voice method from with in it?

class creature:
def __init__(self):
self.noise=""
def voice(self):
return "voice:" + self.noise

class dog(creature):
def __init__(self):
self.noise="bar k"

def voice(self):
print "brace your self:"
I did forget to mention that in 'dog"s' __init__ you had better call
creature's __init__. You might make it look like this:

def __init__(self):
self.noise = 'bark'
creature.__init __(self)

There's a problem with Chaz's __init__() method. Notice that the
creature class's __init__ sets self.noise to the empty string. In this
case, the superclass's __init__() method should be called first:

class dog(creature):
def __init__(self):
creature.__init __(self)
self.noise = "bark"
def voice(self):
print "brace your self:"
creature.voice( self)

--Jason
Very true....I was showing him in "spirit only"...lol.
Chaz.
Aug 29 '06 #7

Chaz Ginger wrote:
Chaz Ginger wrote:
glenn wrote:
hi - Im quite new to python, wondering if anyone can help me understand
something about inheritance here. In this trivial example, how could I
modify the voice method of 'dog' to call the base class 'creatures'
voice method from with in it?

class creature:
def __init__(self):
self.noise=""
def voice(self):
return "voice:" + self.noise

class dog(creature):
def __init__(self):
self.noise="bar k"

def voice(self):
print "brace your self:"

thanks
glenn
Try this:

class dog(creature):
.....
def voice(self):
print "brace your self:"
creature.voice( self)

This should do it.
I did forget to mention that in 'dog"s' __init__ you had better call
creature's __init__. You might make it look like this:

def __init__(self):
self.noise = 'bark'
creature.__init __(self)

There is another approach - using Superclass - but I will leave that
exercise to the reader.
first tip worked - funny thing was I =thought= I done that, but clearly
not - so thanks was going mad.
Superclass?... ok will look into this
thanks for reply(s)
Glenn

Aug 30 '06 #8

Bruno Desthuilliers wrote:
glenn wrote:
hi - Im quite new to python, wondering if anyone can help me understand
something about inheritance here. In this trivial example, how could I
modify the voice method of 'dog' to call the base class 'creatures'
voice method from with in it?

class creature:
def __init__(self):
self.noise=""
def voice(self):
return "voice:" + self.noise

class dog(creature):
def __init__(self):
self.noise="bar k"

def voice(self):
print "brace your self:"


<ot>
It might be better to use newstyle classes if you can. Also, the
convention is to use CamelCase for classes names (unless you have a
strong reason to do otherwise).
</ot>

Here you could use a class attribute to provide a default:

class Creature(object ):
noise = ""

def voice(self):
return "voice:" + self.noise
class Dog(Creature):
noise="bark"

def voice(self):
print "brace your self:"
return Creature.voice( self)
# can also use this instead, cf the Fine Manual
return super(Dog, self).voice()

My 2 cents
ohh - interesting. Thanks for the camelCase tip - dont have a good
reason to do otherwise, just bad habits.
so for your $.02 do you see this as being, umm, superior in anyway to
creature.voice( )?

glenn

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom. gro'.split('@')])"
Aug 30 '06 #9
Hi Roberto
If you want dog.voice() to just print "voice: bark", you just have to omit
the voice method for the dog class: it will be inherited from creature.
I would have thought this would be correct, but in this case, plus in
others im playin with, I get this issue:
-----------------------
given animal.py is:
class creature:
def __init__(self):
self.noise=""
def voice(self):
return "voice:" + self.noise

class dog(creature):
def __init__(self):
self.noise="bar k"

then I get this outcome...
>>>import animal
beagle=animal .dog
beagle.voice( )
Traceback (most recent call last):
File "<input>", line 1, in ?
TypeError: unbound method voice() must be called with dog instance as
first argument (got nothing instead)
>>>
------------------------
So I guess it wants something in position of self?

any idea what Im doing wrong? - this would be very handy as its a point
Im stymied on a couple of 'projects'
thanks
Glenn
If you want dog.voice() to do something else, you can call superclass'
method like this:

def voice(self):
creature.voice( self)
print "brace your self"
any_other_magic ()
HTH
--
Roberto Bonvallet
Aug 30 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
2846
by: Bryan Parkoff | last post by:
I find an interesting issue that one base class has only one copy for each derived class. It looks like that one base class will be copied into three base classes while derived class from base class is executed. It means that three derived classes are pointed to a separated copy of base class. I do not allow second and third copy of base...
5
2552
by: Nathan Bullock | last post by:
Hi, I have a base class, say Base and there are two classes, say Class1 and Class2 which are derived from Base. Is there any way for me, say from a static method in Base, to get a list of all classes derived from Base? class abstract Base { static public ArrayList AllDerived()
13
2562
by: z. f. | last post by:
Hi, i have a class that is derived from System.Web.UI.Page, and this is the class i use in my application as PageBase. all other page classes are deriverd from my PageBase instead of the original System.Web.UI.Page in order to have common checks in the page base. i make securirty checks in the page base page_load event. if the security...
9
5185
by: Sean Kirkpatrick | last post by:
To my eye, there doesn't seem to be a whole lot of difference between the two of them from a functional point of view. Can someone give me a good explanation of why one vs the other? Sean
5
3600
by: eiji | last post by:
Hi folks, I hope this is not "off topic"! :-) Consider the next code: /* Declarations of types that could become platform-dependent */ #define MyChar char #define MyInt int
8
1975
by: Jackson | last post by:
I want a class that will determine its base class by the argument passed in. What I am about to write _does_not_work_, but it shows what I am trying to do. class ABC(some_super): def __init__(self,some_super): some_super.__init__(self) if some_super == list: self.append('ABC')
26
5342
by: nyathancha | last post by:
Hi, How Do I create an instance of a derived class from an instance of a base class, essentially wrapping up an existing base class with some additional functionality. The reason I need this is because I am not always able to control/create all the different constructors the base class has. My problem can be described in code as follows ... ...
15
3824
by: Bob Johnson | last post by:
I have a base class that must have a member variable populated by, and only by, derived classes. It appears that if I declare the variable as "internal protected" then the base class *can* populate the variable, but the population is not *required* by the derived class (which must be the case). What would meet the requirements is if I...
2
2397
by: cmonthenet | last post by:
Hello, I searched for an answer to my question and found similar posts, but none that quite addressed the issue I am trying to resolve. Essentially, it seems like I need something like a virtual static function (which I know is illegal), but, is there a way to provide something similar? The class that is the target of my inquiry is a...
0
7624
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7547
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8060
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7589
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7907
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6178
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3576
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2040
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1161
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.