[Python-Dev] None as a keyword / class methods

gvwilson@nevex.com gvwilson@nevex.com
Tue, 28 Mar 2000 10:45:10 -0500 (EST)


> > Greg Wilson
> > If None becomes a keyword, I would like to ask whether it could be
> > used to signal that a method is a class method, as opposed to an
> > instance method:

> I'd like to know what you mean by "class" method. (I do know C++ and
> Java, so I have some idea...). Specifically, my question is: how does
> a class method access class variables? They can't be totally
> unqualified (because that's very unpythonic). If they are qualified by
> the class's name, I see it as a very mild improvement on the current
> situation. You could suggest, for example, to qualify class variables
> by "class" (so you'd do things like:
>
> 	class.x = 1
>
> ), but I'm not sure I like it. On the whole, I think it is a much
> bigger issue on how be denote class methods.

I don't like overloading the word 'class' this way, as it makes it
difficult to distinguish a parent's 'foo' member and a child's 'foo'
member:

class Parent:
    foo = 3
    ...other stuff...

class Child(Parent):
    foo = 9
    def test():
        print class.foo   # obviously 9, but how to get 3?

I think that using the class's name instead of 'self' will be easy to
explain, will look like it belongs in the language, will be unlikely to
lead to errors, and will handle multiple inheritance with ease:

class Child(Parent):
    foo = 9
    def test():
        print Child.foo   # 9
        print Parent.foo  # 3

> Also, one slight problem with your method of denoting class methods:
> currently, it is possible to add instance method at run time to a
> class by something like
> 
> class C:
> 	pass
> 
> def foo(self):
> 	pass
> 
> C.foo = foo
> 
> In your suggestion, how do you view the possiblity of adding class
> methods to a class? (Note that "foo", above, is also perfectly usable
> as a plain function).

Hm, I hadn't thought of this... :-(

> > I'd also like to ask (separately) that assignment to None be defined as a
> > no-op, so that programmers can write:
> > 
> >     year, month, None, None, None, None, weekday, None, None = gmtime(time())
> > 
> > instead of having to create throw-away variables to fill in slots in
> > tuples that they don't care about.
> 
> Currently, I use "_" for that purpose, after I heard the idea from
> Fredrik Lundh.

I do the same thing when I need to; I just thought that making assignment
to "None" special would formalize this in a readable way.