Mailing List Archive

Newbie class questions
I have just started to learn Python (v1.5.2), and have some questions
about classes. Here is something I prepared earlier...

--- cut ---
def y(x): return 'a global function y(x)'
def g0(): return 'some arbitrary function (no args)'
def g1(x): return 'another arbitrary function (one arg)'
i = 0
class MyClass:
"""A simple class demonstrating scope."""
i = 12345
def __init__(self):
print '\n\tI output, therefore I am.\n'
def y(x):
return 'default class method y'
def f(x):
# i = 7 # hides global
print 'Global variable:', i
print 'Class variable:', MyClass.i
print 'Instance variable:', x.i
print 'Global function:', y(x)
print 'Class method:', MyClass.y(x) # explicit instance arg.
print 'Instance method:', x.y() # implicit instance arg.
return
def q():
"""This method is inaccessible - should be a parse-time error"""
return

t = MyClass()
print "Doc:", t.__doc__
t.f()
print "\nModified:"
i = 21
t.i = 169
#t.y = g1
t.y = g0
t.f()
--- cut ---

BTW I think an example class like this in the tutorial would help
explain scope, but there are also a few things I don't understand here
(probably my C++ background).

(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?

(2) Why doesn't t.y = g1 work? How am I meant to access instance
attributes in g0 without an instance argument?

(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?

Cheers,
Toby
--
Toby Kelsey
Newbie class questions [ In reply to ]
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
Newbie class questions [ In reply to ]
Toby Kelsey wrote:
>
> (1) Is there a reason why I cannot call MyClass.q()? Why require a
> meaningless instance arg to a class-static method?

There is no such thing as a class-static method in Python.
(There used to be, but Guido noticed that we were using
them and put some code in to stop them from working :-)

There are workarounds, but most of the time it's simpler
and clearer just to use a module-level function instead.

Greg