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

David Broadwell anti-spam.dbroadwell at mindspring.com
Mon Apr 28 12:54:38 EDT 2003


BTW: Thank you, You do some good responces to the group. I especially liked
the 'forgot ()' thread solution even though i'm sure you got flames for it.
I'll have to read that 'primer' on metaclasses ... loooks usefull and
potentially code saving.

<posted & mailed>

>> class foo:

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

>>

>> def baz():

>> for item in bar: print item

>> boggle()

>> for item in bar: print item

> This baz function just won't work -- its unqualified references to 'bar'

> and 'boggle' can't be solved, and the "signature" (pattern of arguments,

> namely none) is only compatible with a staticmethod or a free function,

> not with an ordinary method as this appears to be.

Ok. (this didn't make sence on the first read)

>> 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)

> No, it's NOT a *global* attribute -- it's an attribute of the CLASS. You

> can access an attribute of a class through any instance of that class, as

> long as the instance hasn't "overridden" the attribute (i.e., bound an

> attribute of the same name as a per-instance attribute on itself).

Oh. That on the other hand maked perfect sence. All i was ever trying to do
was access a variable one level up and was seemingly forced me to a global
to do the assignment. While a list[index] dosen't seem to have that problem,
strings and integers did. (problem with programmer, not language) That is
untill i go all classy. And that is about exactly what i might do.

>>>>> def boggle(self):

>> self.bar[1] = 'a'

>> self.bar[2] = 'r'

> However, this function needs an argument. You're not passing that

> argument when you call it, thus, boom.

Except within an instance of a class when self is defined? You see i did try
to have boggle called with self and got that little complaint in the
interpreter.

> Question 3: So what exactly does my code have to look like to work?

> Question 4: Is this notation only valid inside a class?

> You can use the "notation" wherever you want, but you cannot call a

> free function without arguments if it wants an argument.

But you can call a class method from an instancce of a class without
arguments and 'self' is required to be received?

> To make the above-exemplified free-standing boogle function work,

> you would have to pass it an object having an attribute named bar.

> In this case, the object in question is the module in which the

> functions baz and boggle live. You can get at the module object

> in the caller, though it's a rather fishy usage IMHO:

>>>> def baz():

> for item in bar: print item

> import sys

> boggle(sys.modules[__name__])

> for item in bar: print item

Oh, a non class instance answer ... fish for the name. But in some cases
will save me a global vaiable declaration, which while simple, i'm NOT fond
of for some reason or other. And in a few cases it will save a STACK of
global variable declarations, which offend my sence of namespace smoothness
... any only requires a bit of fish sauce.






More information about the Python-list mailing list