[Tutor] question about OOP (or just grammer thing) ?

alan.gauld@bt.com alan.gauld@bt.com
Fri, 28 Jun 2002 14:58:02 +0100


> And All I know about programming language was C.
> That means I'm also not used to the OOP concept.

OK, You might like to have a quck look at my OOP page 
in my tutor....

> class HelloButton(Button):
>     def __init__(self, parent=None, **config):
>         Button.__init__(self, parent, config)
>         self.pack()
>         self.config(command=self.callback)

> 1. In the above example, variable named 'config' is used in 3 places. 
> Are they all same or different?

The nmame config is used 3 times.

The first is to specify a parameter to the init method. 
The ** bit means it can actually be multiple arguments which will 
all be treated as one by init.

The second is the use of that same config parameter being passed 
to the Button's init method. If multiple args are passed in to 
your init they will all be passed as part of config to Button.init

The 3rd is a method call to the inherited config method of Button.
config as a method takes keyword style argument ansd sets the 
value within its class. Thus in this case you asre setting the 
command property ogf your Button to be the callback method.

> 
> 2. I guess they're not all same. 

One and Two are virtually the same, the third is entirely different.


>     def __init__(self, parent=None, **config):
>         Button.__init__(self, parent, config)
> I guess variable config in above 2 lines are same one.

Correct
> If then, why '**config' is used in __init__ method ? 

When we call MyButton construvctor like:

b = MyButton(tk, text="Hello", width=45, height=50)

everything after tk gets passed as the config argument.
It treats the 3 arguments as a single argument to init.

HTH,

Alan g.
Author of the 'Learning to Program' web site
http://www.freenetpages.co.uk/hp/alan.gauld