Re: NEWBIE: Sub-Classes
From: Gabriel Genellina (gagenellina_at_softlab.com.ar)
Date: 12/31/03
- Next message: Tim Churches: "Formal reference for Python"
- Previous message: Aahz: "Re: Tkinter and OS X 10.3?"
- In reply to: engsolnom_at_ipns.com: "NEWBIE: Sub-Classes"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Tim Churches: "Formal reference for Python"
- Previous message: Aahz: "Re: Tkinter and OS X 10.3?"
- In reply to: engsolnom_at_ipns.com: "NEWBIE: Sub-Classes"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|