[Tutor] questions from stupid n00b

Anastasia Rohner anastasia at shutupandsitdown.com
Mon Sep 6 20:26:56 CEST 2004


Hi! I'm new. I'm trying to learn Python by reading the tutorial that's in the help file that's downloaded w/ the interpretor. I really like how all the stuff in the tutorial is short, sweet, and to the point. However, since Creature is not a real programmer (yet), some stuff in the tutorial got her a bit confused:

1. Tutorial section 7.4.1 talks about default value arguments. In the 2 examples:

"

def f(a, L=[]):
    L.append(a)
    return L

print f(1)
print f(2)
print f(3)

This will print 


[1]
[1, 2]
[1, 2, 3]

If you don't want the default to be shared between subsequent calls, you can write the function like this instead: 


def f(a, L=None):
    if L is None:
        L = []
    L.append(a)
    return L

"

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.

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?

3. (9.8) What is the difference between these two:

raise Class, instance

raise instance (or raise instance.__class__, instance)

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?

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

That's all for now.  Sorry to ask so many questions at once :)

Thanks in advance ^_^

Creature


More information about the Tutor mailing list