Confused with classmethods

Steve Holden steve at holdenweb.com
Fri Mar 11 16:43:23 CET 2005


jfj wrote:
> Diez B. Roggisch wrote:
> 
>>> Moreover the documentation sais that if the first argument is an 
>>> instance, its class will be
>>> used for the classmethod.  OTOH, "class scope" is not a real thing in
>>> python.  It is just some code which is executed and then we get its
>>> locals and use it on Class(localsDict, BasesTuple, ClassName) to make
>>> a new class, it seems.  So we can create a classmethod in any scope
>>> and then just attach it to a class's dictionary.
>>
>>
>>
>> I'd still call the code executed inside a class statement block a 
>> "scope" -
>> for example in a def-statement the scope is the current frame of 
>> execution,
>> so
>> def foo():
>>    bar = "baz"
>>
> 
> Absolutely.  In fact here is another interesting case which can lead to
> nice recipies:
> 
> class B:
>     print "Making class B!"
>     if blah:
>         def f(self): print self
>     else
>         def f(self): raise StopIteration
>     print "My locals are:",locals(),"and I will make a class from them"
> 
>> makes the bar part of the frames local variables. Scopes just exchange or
>> stack the  dicts for name lookup.
>>
> 
> 
> Well, actually, to put things right, in python there are only *two*
> real scopes: global scope and local scope.
> 
Not strictly true. There's builtins right at the back, and nowadays 
inside a nested function you have a nested set of local scopes.

sholden at bigboy ~
$ cat test88.py
#
# Namespace testing
#
x = 33
y = 44

def f1():
   def f2():
     y = 33
     print "f2", x, y
   x = 22
   print "f1", x, y
   f2()
   print "f1", x, y
print "main", x, y
f1()
print "main", x, y


sholden at bigboy ~
$ python test88.py
main 33 44
f1 22 44
f2 22 33
f1 22 44
main 33 44

sholden at bigboy ~
> I remember that when I was a newbie I was confused by this.
> The thing is that there is no nesting of scopes in reality.
> 
Really?

> Just to help other newbies avoid the confusion, I believe it would
> be better to say, from the start, that:
> 
> - There definitelly is no nesting of scopes for if/for/while/try
> blocks.
> 
Correct.

> - There is no nesting of scopes when nested functions reference
> stuff from the enclosing function.  It looks like there is but
> there isn't because then we should be able to say:
> 
>     exec "some code" in locals(), cellvars(), globals()
> 
> which we can't and proves that referencing variables from the enclosing
> function is indeed a questionable feature.  It all happens because the
> parser detects that there are variables with that name in the enclosing
> functions and creates special cellvars...
> 
I believe you are coinfusing restrictions on exec syntax with 
restrictions on namespace nexting.

> - No nesting of scopes for classes because we *have* to use 'self'
> (which is a good thing IMHO).
> 
regards
  Steve
-- 
Steve Holden        +1 703 861 4237  +1 800 494 3119
Holden Web LLC             http://www.holdenweb.com/
Python Web Programming  http://pydish.holdenweb.com/




More information about the Python-list mailing list