Unification of Methods and Functions

David MacQuigg dmq at gain.com
Tue Jun 1 07:43:26 EDT 2004


On 1 Jun 2004 09:57:28 GMT, Duncan Booth <me at privacy.net> wrote:

>David MacQuigg <dmq at gain.com> wrote in
>news:0dqkb09bc54jlo8m9aqcosei5p7olccvec at 4ax.com: 

< example assuming the desired method is defined in Shape: >

>  Shape.fromCenterAndSize(Rectangle, 10, 10, 3, 4)
>  Shape.fromCenterAndSize(Ellipse, 10, 10, 3, 4)
>  Shape.fromCenterAndSize(OffsetRectangle, 10, 10, 3, 4)
>  Shape.fromCenterAndSize(TextLabel, 10, 10, 3, 4)

< snip long discussion showing why this is not a good assumption. >

>  Rectangle.fromCenterAndSize(Rectangle, 10, 10, 3, 4)
>  Ellipse.fromCenterAndSize(Ellipse, 10, 10, 3, 4)
>  OffsetRectangle.fromCenterAndSize(OffsetRectangle, 10, 10, 3, 4)
>  TextLabel.fromCenterAndSize(TextLabel, 10, 10, 3, 4)
>
>Now we have a consistent interface again. The only problem with this is
>that we have duplication in the call. That is easily fixed though by
>switching to class methods and you get back to my code.

I can still replace this more easily with:

for cls in Rectangle,Ellipse,OffsetRectangle,Textlabel:
   cls.fromCenterAndSize(cls, 10, 10, 3, 4)

However, if you change this example so all the arguments are
different, then you have made your point.  Class methods can avoid
much duplication of class names.

>One more point. You said in an earlier post:
>
>> I would change the classmethods to staticmethods, however, and avoid
>> the need to teach classmethods. 
>
>If you are going to teach your students how to create objects in Python you 
>will need to explain the __new__ method. __new__ is automatically a class 
>method, so there is no way you can avoid teaching class methods. You can 
>however avoid teaching static methods.

Actually, __new__ is a staticmethod.  According to GvR in
http://python.org/2.2.3/descrintro.html#__new__
'''
Factoid: __new__ is a static method, not a class method. I initially
thought it would have to be a class method, and that's why I added the
classmethod primitive. Unfortunately, with class methods, upcalls
don't work right in this case, so I had to make it a static method
with an explicit class as its first argument. Ironically, there are
now no known uses for class methods in the Python distribution (other
than in the test suite). However, class methods are still useful in
other places, for example, to program inheritable alternate
constructors. 
'''

Static methods are needed whenever you want to call a method without
an instance.  True, we could do this with a class method and just
ignore the class, but the strange first argument will need further
explanation, and will seem odd to students seeing lots of static
methods and very few class methods in typical programs.  

I will probably leave class methods as they are in Learning Python,
2nd ed - in an "advanced" section toward the end of the OOP chapters.

I haven't yet decided what to do with __new__ and __metaclass__.  I've
received a good example of using these to generate classes for the
Animals examples, but the Python documentation is poor, and I think I
can accomplish the purpose of automatically generating classes with
correct attributes, using "factory functions" as your example
illustrates.

-- Dave




More information about the Python-list mailing list