Re: NEWBIE: Sub-Classes

From: Gabriel Genellina (gagenellina_at_softlab.com.ar)
Date: 12/31/03


Date: Wed, 31 Dec 2003 01:07:10 -0300
To: engsolnom@ipns.com

At 30/12/2003 17:40, you wrote:

>I'm a bit baffled. In the code below, I expected that the import in
>tst_sub.py would expose all the
>contents of dir_a.py. But, as it turns out...it doesn't.

"import dir_a" puts dir_a into the module's namespace, so dir_a has a
meaning from this point on. It's not the same as "from dir_a import *"
which puts all public symbols from dir_a into the module's namespace.
A simple example: the builtin module math contains a sqrt() function. You
can use:

import math
print math.sqrt(2)

or:

from math import sqrt
print sqrt(2)

or:

from math import *
print sqrt(2)

The last form is not recommended; the second is the best way (as it makes
explicit what you are importing, and from where)

>I can understand (I think) why B has to be qualified in class A(dir_a.B),

Good, the answer should be: "because I imported dir_a".
If you had written: from dir_a import B, then you could use B alone
wherever you use dir_a.B now.

>but why is dir_a.B.class_B
>required?

You have two things named 'class_B': one is an attribute of class B (*not*
an instance attribute); another is an attribute of B instances that only
exists after executing the method B_method_1
Class attributes act like defaults for instance attributes in a "get"
context: if b is an instance of class B, b.class_B == 21 before executing
B_method_1, and b.class_B == 19 after.
b.class_B = 19 *creates* an instance attribute named "class_B" with value
19; you never modify the class attribute this way. (You could do that, of
course, if that's what you really want to do).

>Also, how can I expose self.B_self to A?

In Python you must *explicitely* call the inherited __init__ method.

class A(dir_a.B):
     def __init__ (self):
         dir_a.B.__init__(self)
         ...

Then, you can access self.B_self (which is initialized in B.__init__) as usual.

Read the Python tutorial about these topics, it's good.

Gabriel Genellina
Softlab SRL



Relevant Pages

  • Re: complilation error
    ... I am trying to compile apache 2.0.59. ... Unfortunately I get the following error when executing make ... among others the sqrt function existence ... The sqrt and other math functions are in 'math ...
    (AIX-L)
  • Re: complilation error
    ... I am trying to compile apache 2.0.59. ... Unfortunately I get the following error when executing make ... among others the sqrt function existence ... The sqrt and other math functions are in 'math ...
    (AIX-L)
  • Re: About Tcl syntax...
    ... because it is in my head from another languages. ... Googie wrote: ... when they need to do some math. ... puts $array) ...
    (comp.lang.tcl)
  • how to overload sqrt in a module?
    ... I would like to write a module that provides some mathematical functions on top of those defined in math, cmath etc. but I would like to make it work with "any" type that overloads the math functions. ... I understand why sqrt is not in scope, but my question is: what is the best way to do this? ... def g: ... because of the special member __abs__ attached to the type. ...
    (comp.lang.python)
  • Re: Beginner with a question.
    ... puts 'Try '+ finalnum +' instead.' ... a "puts finalnum" works.) ... You're trying to do math and concatenation of strings at the same time. ...
    (comp.lang.ruby)