Tkinter question: master?

Eric Brunel eric.brunel at pragmadev.com
Fri Mar 8 06:08:12 EST 2002


<posted & mailed>

chajadan wrote:
[snip]
> class Application(Frame):
> 
> def __init__(self, master):
> Frame.__init__(self, master)
[snip]
> in "def __init__(self, master):", self refers to this class, i.e.
> Application (though how those are inherently linked [by name or arguement
> position--assuming position] I have no clue. master will be set equal to
> whatever the next arguement is, which seems to be Frame...

Nope: "self" does *not* refer to the class, neither do "master" refer to 
Frame. "self" refers to the instance being built. There's no link at all 
between the arguments in the __init__ method and what you specified in the 
class declaration (Application(Frame)), which describes the inherited 
classes.

Consider this far simpler example:

class C1:
  def __init__(self, a, b):
    self.x = a + b

class C2(C1):
  def __init__(self, a, b, c):
    C1.__init__(self, a, b)
    self.y = c

C1 does not inherit from anything, so you define it like above: "class C1" 
with nothing behind. The method __init__ is defined with two parameters, 
that will be passed as arguments when the object is built. So if you have 
to build an instance of C1, you'll have to do that:

o1 = C1(2, 4)

(Try to pass any other number of arguments, and you'll see it fails...)

Now C2 inherits from C1, so its definition is written "class C2(C1)". But 
its __init__ method takes 3 parameters, so when you create an instance of 
C2, you'll write:

o2 = C2(1, 3, 5)

I see what you find confusing: in the class definition C2(C1) means 
"inheritance", but in object creation, C2(1, 3, 5) means "call to the 
__init__ method". The notations are similar, but the meaning are different. 
Which is indeed even more confusing when you inherit from one class and the 
__init__ method has one argument, like in your example.

> the (Frame) in the class definition must just be for
> inheritance purposes and have ~NOTHING~ to do with arguements

You're right on that.

HTH
 - eric -




More information about the Python-list mailing list