Re: [Tutor] static methods and class methods

2008-06-13 Thread wesley chun
[static methods, class methods]

 Both of these are rarely used; I don't think I have ever written a
 class method in live code. I have used staticmethods as a convenient
 way to put a function into a class namespace.

this is someone out of the flow of the current thread now, but i
forgot to mention in my previous post that, like kent, i too, have not
used either of these much in practice on the past 11yrs of full-time
Python development.

class methods: the only time i've ever done a class method is when
subclassing an immutable type, i.e., __new__() is a cls method, but
that was for the book. i've never done one in practice.

static methods: may have done 1 or 2 since they came out in 2.2, but
in reality, if general code, they stay as global functions. if i
really want a function defined in a class definition, i may make one.
but in practice, i often find that the functions that are most
suitable as static methods end up being just *inner* functions defined
inside a (regular) method.

just my $0.02,
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Core Python Programming, Prentice Hall, (c)2007,2001
 http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] static methods and class methods

2008-06-12 Thread Kent Johnson
On Thu, Jun 12, 2008 at 7:48 PM, Alan Gauld [EMAIL PROTECTED] wrote:
 Kent Johnson [EMAIL PROTECTED] wrote

  I am still unsure of the difference of static and class methods.

No, that wasn't me, it was Christopher Spears.

 You are not alone, it is confusing!

 A class method receives the class it was called on as the first
 argument. This can be useful with subclasses. A staticmethod doesn't
 get a a class or instance argument. It is just a way to put a plain
 function into the scope of a class.

 And that's the definition of the difference in Python.

Yes, that is what I said :-)

 Python started off implementing static methods then later
 developed the sligtly more powerfull and flexible class methods and
 rather than lose backward compatibility called them classmethod.

Are you sure? They were both introduced in Python 2.2 with new-style classes:
http://www.python.org/doc/2.2.3/whatsnew/sect-rellinks.html

PEP 252 has an entire section on Static methods and class methods:
http://www.python.org/dev/peps/pep-0252/

 So in Python we have two ways of doing more or less the same
 (conceptual) thing.

?? But they are not the same thing.

 In contrast most of my major projects have used class methods
 in some way or other. Often in conjunction with factory classes or
 as factory methods, but also as a persistence mechanism, a
 streaming mechanism or as a database access technique.

I would be interested in an example if you care to share. I have used,
or can imagine using, static methods that way but not class methods.

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] static methods and class methods

2008-06-12 Thread Alan Gauld
Kent Johnson [EMAIL PROTECTED] wrote in message 
news:[EMAIL PROTECTED]
On Thu, Jun 12, 2008 at 7:48 PM, Alan Gauld 
[EMAIL PROTECTED] wrote:

Kent Johnson [EMAIL PROTECTED] wrote


 I am still unsure of the difference of static and class methods.


No, that wasn't me, it was Christopher Spears.


Sorry Kent, I got my header editing a bit mixed up.


Python started off implementing static methods then later
developed the sligtly more powerfull and flexible class methods 
and

rather than lose backward compatibility called them classmethod.


Are you sure? They were both introduced in Python 2.2 with new-style 
classes:

http://www.python.org/doc/2.2.3/whatsnew/sect-rellinks.html


Yes, but I'm fairkly sure I read an article by guido in a magazine
or maybe the O'Reilly blogsite that they came up with the
classmethod idea after they had already done the staticmethod
stuff so they kept both.

But I may be hallucinating again! :-)


So in Python we have two ways of doing more or less the same
(conceptual) thing.


?? But they are not the same thing.


Conceptually they are both ways of defining a method that
applies at the class level and could be used to implement
class wide behavior. Thats what I mean. If you want to build
a method to determine how many instances are active at
any time then you could use either a staticmethod or a
classmethod to do it. Most languages only give you one
way. Python, despite its mantra, actually gives 2 ways to
do it in this case.


In contrast most of my major projects have used class methods


I would be interested in an example if you care to share. I have 
used,

or can imagine using, static methods that way but not class methods.


Notice the space. I mean class methods in the OOP sense
not specifically classmethods in the Python sense. I have never
found anything in Python that I needed a classmethod rather
than a staticmethod, although if I were writing dynamic AI type
stuff I suspect I could find examples. But in almost any major
project I will have a use for class based methods

One specific example of how I've used classmethods in a
networked client/server app was where we had a GUI client talking
to a server. Both GUI and server used the same class interaces
but the GUI implementation of the interface was just a pass through
to the server. When the GUI messages reached the server a listener
picked the message from a socket, decoded the target class and
invoked a getInstance() class method. The class method extracted
the instance ID from the message. If the object was in memory
(an object pool) it called the requested method on the instance
and returned the response to the GUI. If the instance was not
in memory but an instanceID was provided it fetched the instance
from the database and returned the called method response. If no
ID was given it created a new instance, saved it to the database
and then called the method on the new instance.

If the message was a query then it was performed by another
class method which just ran a SELECT on the underlying database
tables.

Its pretty obvious that the class methods here were non trivial.
We also had garbage collection and pool management class
methods too. We had about 100 data classes like this with
the core functionality in a super DBClass' class methods which
were in turn called by each subclass' class methods along
with their individual attribute wrappers. To add to the interest
the data was stored in multiple databases on different boxes
around the globe, so the class methods also had to figure out
which database instance to talk to when fetching or writing
objects...

Needless to say this was before the days or Object databases
and ORMs! But the final system managed millions of objects
with thousands of users in around a dozen countries, and did
so for many years. (And all in C++ :-)

Hope that makes sense!


--
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] static methods and class methods

2008-06-11 Thread Kent Johnson
On Wed, Jun 11, 2008 at 12:17 AM, Christopher Spears
[EMAIL PROTECTED] wrote:
 tcm.foo
 bound method classobj.foo of class __main__.TestClassMethod at 0xb7da0f2c


 According to the author, the result for typing in 'tcm.foo' is

 calling class method foo()
 foo() is part of class: TestClassMethod

Try tcm.foo()

tcm.foo without parentheses is the classmethod itself. You need
parentheses to actually call it.

 Did I do something wrong or is this an error on the book's part?  
 Intuitively, the answer I received makes more sense to me.  I am still unsure 
 of the difference of static and class methods.  Can someone enlighten me?

A class method receives the class it was called on as the first
argument. This can be useful with subclasses. A staticmethod doesn't
get a a class or instance argument. It is just a way to put a plain
function into the scope of a class.

Both of these are rarely used; I don't think I have ever written a
class method in live code. I have used staticmethods as a convenient
way to put a function into a class namespace.

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] static methods and class methods

2008-06-10 Thread Christopher Spears
I am reading  Wesley Chun's Core Python Programming (2nd Edition) and have  
reached the part on static and class methods.  I typed in the following to 
demonstrate the difference between the two methods:

 class TestClassMethod:
... def foo(cls):
... print 'calling class method foo()'
... print 'foo() is part of class:',cls.__name__
... foo = classmethod(foo)
...

 class TestStaticMethod:
... def foo():
... print 'calling static method foo()'
... foo = staticmethod(foo)
...
 tsm = TestStaticMethod()
 TestStaticMethod.foo()
calling static method foo()
 tcm = TestClassMethod()
 TestClassMethod.foo()
calling class method foo()
foo() is part of class: TestClassMethod
 tcm.foo
bound method classobj.foo of class __main__.TestClassMethod at 0xb7da0f2c


According to the author, the result for typing in 'tcm.foo' is
 
calling class method foo()
foo() is part of class: TestClassMethod

Did I do something wrong or is this an error on the book's part?  Intuitively, 
the answer I received makes more sense to me.  I am still unsure of the 
difference of static and class methods.  Can someone enlighten me?

Thanks!


  
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] static methods and class methods

2008-06-10 Thread Marilyn Davis
On Tue, June 10, 2008 9:17 pm, Christopher Spears wrote:

 I am reading  Wesley Chun's Core Python Programming (2nd Edition) and
 have  reached the part on static and class methods.  I typed in the
 following to demonstrate the difference between the two methods:

 class TestClassMethod:
 ... def foo(cls):
 ... print 'calling class method foo()'
 ... print 'foo() is part of class:',cls.__name__
 ... foo = classmethod(foo)
 ...


 class TestStaticMethod:
 ... def foo():
 ... print 'calling static method foo()'
 ... foo = staticmethod(foo)
 ...

 tsm = TestStaticMethod() TestStaticMethod.foo()

 calling static method foo()
 tcm = TestClassMethod() TestClassMethod.foo()

 calling class method foo() foo() is part of class: TestClassMethod
 tcm.foo
 bound method classobj.foo of class __main__.TestClassMethod at
 0xb7da0f2c



 According to the author, the result for typing in 'tcm.foo' is

You forgot to put () after the method name.  Wesley has them.

Class methods and static methods can be called, even when you don't have
an instance of the class.

I think that that's all there is to it.

With the class method, the interpreter provides the class itself as the
first argument.  With the static, nothing comes in.

What else?

Marilyn Davis



 calling class method foo() foo() is part of class: TestClassMethod

 Did I do something wrong or is this an error on the book's part?
 Intuitively, the answer I received makes more sense to me.  I am still
 unsure of the difference of static and class methods.  Can someone
 enlighten me?

 Thanks!




 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor