refering to base classes

G

glenn

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

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

thanks
glenn
 
C

Chaz Ginger

glenn said:
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="bark"

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.
 
R

Roberto Bonvallet

glenn said:
[...] 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="bark"

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
 
C

Chaz Ginger

Chaz said:
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.
 
B

Bruno Desthuilliers

glenn said:
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="bark"

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
 
J

Jason

Chaz said:
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
 
C

Chaz Ginger

Jason said:
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.
 
G

glenn

Chaz said:
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
 
G

glenn

Bruno said:
<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

 
G

glenn

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

then I get this outcome...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
 
B

Ben Finney

Note that this style is more correctly called TitleCase, since the
first letter of *every* word is capitalised, like in a headline (or
title). "camel case" is different -- see below.
ohh - interesting. Thanks for the camelCase tip - dont have a good
reason to do otherwise, just bad habits.

The style called camelCase (all words run together, capitalise first
letter of every word except the first) is prevalent in Java, where it
denotes the name of an *instance*, in contrast to a *class* which is
named with TitleCase.

The camelCase style is less popular in the Python world, where (as per
PEP 8) instances are named with all lower case, either joinedwords or
separate_by_underscores.
 
S

Steve Holden

glenn said:
Hi Roberto


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

then I get this outcome...
[...]

Shouldn't that be

beagle = animal.dog()

to create an instance?

We've all done it ...

regards
Steve
 
B

Bruno Desthuilliers

Ben said:
Note that this style is more correctly called TitleCase, since the
first letter of *every* word is capitalised, like in a headline (or
title). "camel case" is different -- see below.

Oops, sorry - my mistake.
 
B

Bruno Desthuilliers

glenn said:
Bruno Desthuilliers wrote:
(snip) (snip)

so for your $.02 do you see this as being, umm, superior in anyway to
creature.voice()?

I suppose "this" refers to the use of super() ? If so, I wouldn't say
it's "superior", but it can be helpful with complex inheritence scheme
(something that does'nt happen very frequently in Python), and more
specifically with multiple inheritance. You may want to read this for
more infos:

http://www.python.org/download/releases/2.2.3/descrintro/#mro

HTH
 
G

glenn

Shouldn't that be

beagle = animal.dog()

to create an instance?

We've all done it ...
lol - actually Im confused about this - there seem to be cases where
instantiaing with:
instance=module.classname()
gives me an error, but
instance=module.classname
doesnt - so I got into that habit, except for where I had a constructor
with parameters - except now Im feeling foolish because I cant
replicate the error - which suggests I didnt understand the error
message properly in the first place... arrgh
I guess thats just part of the process of gaining a new language.

glenn
 
G

glenn

thanks - interesting essay/article - a lot in their I've never really
considered - though its only recently ive started playing with multiple
inheritance in any context - thanks for that
 
C

Chaz Ginger

glenn said:
lol - actually Im confused about this - there seem to be cases where
instantiaing with:
instance=module.classname()
gives me an error, but
instance=module.classname
doesnt - so I got into that habit, except for where I had a constructor
with parameters - except now Im feeling foolish because I cant
replicate the error - which suggests I didnt understand the error
message properly in the first place... arrgh
I guess thats just part of the process of gaining a new language.

glenn

module.classname and module.classname() are two different things. If you
use module.classname() you invoke the __new__ and __init__ methods in
the class, and you might get an error from them.

On the other hand module.classname will always work, assuming classname
really exists in module. What you get back is a sort of reference to the
class itself and not an instance of it.
 
B

Bruno Desthuilliers

glenn said:
lol - actually Im confused about this - there seem to be cases where
instantiaing with:
instance=module.classname()
gives me an error, but
instance=module.classname
doesnt -

These two syntaxes are both legal, but yield different results.
<short>
You *have* to use "instance=module.classname()" to instanciate
module.classname. Else what you get is the class itself, not an instance
of it.
</short>

<longer>
In Python, everything (including modules, classes and functions) is an
object.

The dot ('.') is a 'lookup' operator - 'someobj.someattr' tries to
retrieve attribute 'someattr' from object 'someobj' (according to lookup
rules that are explained in the doc) - whatever that attribute is. So
the expression 'module.classname' yields the attribute 'classname' of
object 'module' (or raise an AttributeError). Execution of the statement
'instance = module.classname' binds the name 'instance' to whatever
expression 'module.classname' yielded - in you case, the class object,
not an instance of it.

Now functions and classes are "callable" objects - which means they
implement the __call__(self, ...) magic method, so you can apply the
call operator to them. Classes are actually callable objects acting as
factory for instances (ie: calling a class object returns an instance of
that class). The expression "someobj.someattr()" *first* lookup for
attribute 'someattr' of object 'someobj', *then* call it.
so I got into that habit, except for where I had a constructor
with parameters

Programming by accident is a well-known antipattern.
- except now Im feeling foolish because I cant
replicate the error - which suggests I didnt understand the error
message properly in the first place...

And that you misunderstood a very important point of Python's syntax.
The call operator (ie parenthesis) is *not* optional.
arrgh
I guess thats just part of the process of gaining a new language.

Probably, yes !-)
 
B

Bruno Desthuilliers

glenn said:
thanks - interesting essay/article - a lot in their I've never really
considered - though its only recently ive started playing with multiple
inheritance in any context - thanks for that

While you're at it (and if not done yet), take some times to read the
whole article - mro is only a little part of the whole thing.
Understanding lookup rules and the descriptor protocol can be of great help.
 
G

Georg Brandl

Chaz said:
module.classname and module.classname() are two different things. If you
use module.classname() you invoke the __new__ and __init__ methods in
the class, and you might get an error from them.

On the other hand module.classname will always work, assuming classname
really exists in module. What you get back is a sort of reference to the
class itself and not an instance of it.

It is not a sort of reference to the class, it is *the class itself*.
.... pass
....
Georg
 

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,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top