Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Newbie inheritance question.

Reply
Thread Tools

Newbie inheritance question.

 
 
Jeremy Bowers
Guest
Posts: n/a
 
      01-16-2005
On Sun, 16 Jan 2005 22:08:13 +0800, bwobbones wrote:

> Hi all,
>
> I'm a java programmer struggling to come to terms with python - bear
> with me!


Christophe already identified the problem, I wanted to address another
Javaism in your code, for your educational benefit.

Speaking "idiomatically" (since this is all small sample code), in Python
you'd normally code your driver as follows:

----------
import two

t = two() # or something, there's a name conflict here, too many "two"s
t.printHello()
t.printTestVar()
----------

Things that aren't objects shouldn't be forced to be objects and I don't
think there is any shame in having a non-OO top level. (I go on about this
at http://www.jerf.org/writings/bowersLaw.html#4 in more detail.)


 
Reply With Quote
 
 
 
 
bwobbones
Guest
Posts: n/a
 
      01-16-2005
Hi all,

I'm a java programmer struggling to come to terms with python - bear
with me!

I'm trying to subclass a class, and I want to be able to see it's
attributes also. Here are my classes:

one.py:
*****************************
class one:
def __init__(self):
print "one"
self.testVar = 1

def printHello(self):
print "hello"
*****************************

two.py
*****************************
from one import one

class two(one):
def __init__(self):
print "two"

def printTestVar(self):
print "testVar: " + str(self.testVar)
*****************************

and the driver to make it work:
*****************************
from two import two

class driver:
def go(self):
print "driver"
self.two = two()
self.two.printHello()
self.two.printTestVar()

if __name__ == '__main__':
d = driver()
d.go()
*****************************

the "self.two.printTestVar()" call doesn't work: shouldn't two.py have
access to all of it's parents attributes? In java I would make the
testVar protected so it's children could see it. How do I do this in
python?

Thanks,

Bones

 
Reply With Quote
 
 
 
 
Christophe Cavalaria
Guest
Posts: n/a
 
      01-16-2005
bwobbones wrote:

> Hi all,
>
> I'm a java programmer struggling to come to terms with python - bear
> with me!
>
> I'm trying to subclass a class, and I want to be able to see it's
> attributes also. Here are my classes:
>
> two.py
> *****************************
> from one import one
>
> class two(one):
> def __init__(self):
> print "two"
>
> def printTestVar(self):
> print "testVar: " + str(self.testVar)
> *****************************


You simply forgot to call the parent __init__. You have to do that
explicitely in Python.
 
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
C++ Struct inheritance against class inheritance johnsonlau C++ 1 07-21-2008 04:58 PM
Interface inheritance vs Implementation inheritance. Daniel Pitts Java 27 02-27-2008 01:37 AM
Private Inheritance and Publice Inheritance karthikbalaguru C++ 9 09-10-2007 01:05 PM
mul. inheritance & overloading operator new/delete solved by virtual base inheritance? cppsks C++ 0 10-27-2004 07:49 PM
Private access modifier and Inheritance (Inheritance implementation in Java) maxw_cc Java 1 12-21-2003 11:38 AM



Advertisments