pychecker too self-centric?
mis6 at pitt.edu
Tue Feb 4 16:39:16 CET 2003
I have just downloaded pychecker (version 0.8.12) and discovered that it
is a little too much self-centric for my taste.
Consider the following example, taken from "Unifying types and classes in
class inch(float): #Guido's example
"Convert from inch to meter."
def __new__(cls, arg=0.0):
return float.__new__(cls, arg*0.0254)
Pychecker will complain since the first argument of '__new__' is 'cls' and
new.py:3: self is not first method argument
However, in this case, I would say that calling the first argument 'cls'
is much more correct than calling it 'self', since '__new__' is a
static method, i.e. the first argument is not an instance of the class.
In general, I think pychecker should be able to distinguish static methods
(and class methods, where the first argument should be called 'cls') from
traditional methods. Is this going to happen in a future version?
In addition, it would be nice if pychecker could distinguish methods
in a normal class from methods in a metaclass. The first argument for
a method in a metaclass should be 'cls', not 'self'. I mean, in principle it
is perfectly correct to call it 'self', since at the very end a metaclass *is*
a class, but still I think that calling it 'cls' enhances a lot the clarity
and avoids any possible confusion.
I have a question of style, too.
When I use __new__ in a metaclass, I have decided to use as its
first argument 'meta'. Look for instance at the following example:
"Adds an attribute serialnumber to its instances"
dic['serialnumber']='This is instance #%s of MetaCounter' %meta.counter
#Creates two instances of Metacounter
class C: __metaclass__=MetaCounter
class D(C): pass
print C.number # This is instance #1 of MetaCounter
print D.number # This is instance #2 of MetaCounter
I thinks is would be confusing to call the first argument 'cls', for the
same reason it would be confusing to call 'self' the first argument of
'__new__' in a class: the first argument of '__new__' in a class is the
class, not an instance of it, whereas the first argument of '__new__'
in a metaclass is the metaclass, not an instance of it.
In summary, I use the following conventions:
in a class in a metaclass
1st arg normal method self cls
1st arg __new__ cls meta
1st arg classmethod cls meta
1st arg staticmethod (not __new__) free choice free choice
I would like to know if other people use these conventions and if there are
style guides on the subject.
Michele Simionato - Dept. of Physics and Astronomy
210 Allen Hall Pittsburgh PA 15260 U.S.A.
Phone: 001-412-624-9041 Fax: 001-412-624-9163
More information about the Python-list