Newbie: ( if good in self: print "Foobar" )

David Broadwell anti-spam.dbroadwell at mindspring.com
Mon Apr 28 00:06:50 EDT 2003


Disclaimer(s):
    I'm a new programmer.
    I'm, a little confuzed.
    I'm working on a 'cut my teeth' program.
    No, it's not any form of classwork.
    This post is long and verbose.

In the course of reading the modules i'm importing to use in my program i've
seen constructions like this:

<code>

class foo:
    bar = ['B','A','R']

    def baz():
        for item in bar: print item
        boggle()
        for item in bar: print item

    def boggle(self):
        self.bar[1] = 'a'
        self.bar[2] = 'r'

</code>

Question 1: Do I read it right that; a classes method called with self can
change the 'global' value without having to type the bloddy global for every
one?!? (ie: the above code not the below code)

<code>

bar = ['B','A','R']

def baz():
    for item in bar: print item
    boggle()
    for item in bar: print item

def boggle():
    global bar
    bar[1] = 'a'
    bar[2] = 'r'

</code>

Question 2: If that is true, what do we call this notation? (anyone got
links?)

Note: In the methods i've read, i have seen the function defined as; def
function (self,arg1,arg2):
I did try to answer the question myself with experimentation, which usually
works, but defining the variable and runctions as they lay and running the
first gave me a traceback (IDLE session paste follows);

>>> bar = ['B','A','R']
>>> def baz():
        for item in bar: print item
        boggle()
        for item in bar: print item
>>> def boggle(self):
        self.bar[1] = 'a'
        self.bar[2] = 'r'
>>> baz()
B
A
R
Traceback (most recent call last):
  File "<pyshell#4>", line 1, in ?
    baz()
  File "<pyshell#2>", line 3, in baz
    boggle()
TypeError: boggle() takes exactly 1 argument (0 given)
>>>

Question 3: So what exactly does my code have to look like to work?
Question 4: Is this notation only valid inside a class?

Brags: I love the cli interface, it's the most usefull thing since my Atari
800 XL (the one with the chipped basic boot prompt that did my homework).

Thanks in advance ...

--

Long live Zork.
    get candle
    light candle
    bask in glow






More information about the Python-list mailing list