[Tutor] questions from stupid n00b

Kent Johnson kent_johnson at skillsoft.com
Tue Sep 7 01:24:24 CEST 2004


At 11:26 AM 9/6/2004 -0700, Anastasia Rohner wrote:
>1. Tutorial section 4.7.1 talks about default value arguments.
><snip>
>What exactly does the if statement change?  I mean, if L is set to the 
>default value defined in the arguments list only during the first run of 
>the function (the way it seems to be in the first example), then on the 
>second run of the second example, L should no longer be None, and bypass 
>the if statement altogether.

In Python, two variables can refer to the same value. (This is called 
aliasing.) If the value is mutable, changing it for one variable changes it 
for both. For example, if I say
 >>> a=[]
 >>> b=a
a and b now refer to the same list, so changing a changes b also:
 >>> a.append(1)
 >>> a
[1]
 >>> b
[1]

This is similar to what happens when a list is used as a default value for 
a function parameter. When a function is created, the default values are 
created and stored as an attribute of the function object (f.func_defaults, 
actually). When the function is called, if a parameter is omitted, the 
default value for that parameter is bound to the parameter name. Now there 
are two variables that refer to the same default value - the function 
attribute containing the default values, and the variable in the function body.

If the default value is mutable, like a list, and the function body changes 
it, the actual default value is changed.

Finally, if the function is called again without the optional parameter, 
the _modified_ default parameter is bound to the new local variable.

For example,
 >>> def f(a, l=[]):
...   l.append(a)
...   return l
...
 >>> f.func_defaults
([],)

The default value is an empty list.

Now call f and the default value will change:
 >>> f(1)
[1]
 >>> f.func_defaults
([1],)

Adding the if statement makes the function create a new list each time it 
is called, so the default value is not changed.

>2. (section 9.6) So the way to make a private variable is to put two 
>underscores in front of the name? Or to do 'underscore, classname, two 
>underscores, variable name'? Are those two used interchangebly for 
>defining the private variable, or is the first one for defining and the 
>second one for calling?

Just use two underscores. If for some reason you need to refer to the 
'private' variable from outside the module it is defined in, use the longer 
form. These variables aren't really that private, but it is a little 
inconvenient to access them from outside and a strong hint that you 
shouldn't do so.

>3. (9.8) What is the difference between these two:
>
>raise Class, instance
>
>raise instance (or raise instance.__class__, instance)

I don't think there is any difference.

>4. (also 9.8) How does this thing work:
>
>class B:
>     pass
>class C(B):
>     pass
>class D(C):
>     pass
>
>for c in [B, C, D]:
>     try:
>         raise c()
>     except D:
>         print "D"
>     except C:
>         print "C"
>     except B:
>         print "B"
>
>I kind of get that in the top part C is a child class of B and D of C, but 
>what does it do for the example? Also, is it just a coincidence that the 
>lower case 'c' is both the loop controll variable and the non-existant 
>function being called in the 'try' block, or are those two connected?

This example is showing that you can have a hierarchy of exceptions. If you 
have multiple except clauses, they are tried in order until one matches.

The loop binds the variable c to each of B, C and D in turn. Calling c() is 
the same as calling B(), C() or D() - a constructor call. B, C and D are 
the exception classes. When B is thrown, it is caught by 'except B', etc.

>5. Creature's stupid question of the month: what does 'container object' mean?

Well, in a general way it is an object that can contain other objects. For 
example a list or dictionary can contain strings, a Tkinter Frame can 
contain Buttons and Labels, an XML Element can contain other Elements...

Kent

>That's all for now.  Sorry to ask so many questions at once :)
>
>Thanks in advance ^_^
>
>Creature
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list