[Baypiggies] Frequently Argued Objections

Alex Martelli aleax at google.com
Tue Jun 24 19:28:10 CEST 2008

On Tue, Jun 24, 2008 at 5:03 AM, Shannon -jj Behrens <jjinux at gmail.com> wrote:
> Your post did bring up one thing I do like about Ruby.  It has a more
> defined notion of class scope at class definition time.  If Python had
> that, I could do something like:
> class B(A):
>    ...
>    if hasattr(B, 'foo'):
>        ...
> That's not possible now, because B doesn't exist until the class is
> finished being defined.  There are workarounds.  It's not a big deal.
> I'm just saying ;)

Why do you consider "a workaround" to code for such weird needs:

>>> class B(object):
...   foo = 23
...   if 'foo' in locals(): print 'foo is', foo
...   else: print 'no foo'
foo is 23

Seems very direct to me -- the class's direct attributes are kept in
the class body's locals() while said body executes, and that's the
dict that's passed to the metaclass's __new__ (and possibly __init__)
(plus of course each base class may have more attributes). This is a
perfectly well defined and documented "notion of scope during class
definition" (even though I've never felt the need to use it within the
class body, as opposed to using it in some metaclass).  What am I


More information about the Baypiggies mailing list