Newbie class questions

Evan Simpson evan at
Fri Jun 11 01:12:35 EDT 1999

Toby Kelsey wrote in message ...
[example snipped]
> (1) Is there a reason why I cannot call MyClass.q()?  Why require a
> meaningless instance arg to a class-static method?  Alternatively
> shouldn't it be an error at parse time?
The answer to all three questions is this:  MyClass.__dict__['q'] is a
perfectly good function with no parameters, and you can call it.  MyClass.q
is that same function magically wrapped as an unbound method of MyClass,
which can't be called since it lacks the required arg.  You could achieve
the same effect by defining 'otherq' elsewhere, then saying 'MyClass.q =
otherq', since the magic lives in the class-attribute access, not the
function itself.

> (2) Why doesn't t.y = g1 work?  How am I meant to access instance
> attributes in g0 without an instance argument?
't.y = g1' works just fine, but it doesn't do what you expected.  Since
method-wrapping magic lives in the class-attribute access rather than the
instance-attribute access, you'll get the same unwrapped g1 out of 't.y'
that you put in, and it will want an arg, while g0 is happy with no args.
If you really want them to act as class methods, you need to 'MyClass.y =
g1'.  Then 't.y' will pass through to 'MyClass.y', then the
instance-attribute access method-binding magic will kick in.  Clear? <wink>

> (3) Some mention was made in the tutorial of the scope rules being in
> flux.  Are any impending changes likely to affect the output of this
> class?
Not that I've heard.

should-be-safe-till-2.0-then-all-bets-are-off-ly y'rs

More information about the Python-list mailing list