[Tutor] static methods and class methods
Kent Johnson
kent37 at tds.net
Wed Jun 11 13:30:52 CEST 2008
On Wed, Jun 11, 2008 at 12:17 AM, Christopher Spears
<cspears2002 at yahoo.com> 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
More information about the Tutor
mailing list