What is class method?

Medardo Rodriguez (Merchise Group) med.swl at gmail.com
Tue Aug 26 17:51:44 EDT 2008


On Tue, Aug 26, 2008 at 4:10 PM, Bruno Desthuilliers
<bdesth.quelquechose at free.quelquepart.fr> wrote:
> In Python, there's *no* relationship between classmethods and metaclasses.

In OOP the concept of meta-class has everything to do with class
methods, regardless if is in Python, SmallTalk or CLOSS. "classmethod"
decorator it's just a syntax sugar structure to define them. There is
no difference (conceptually) on "method1" and "method2":
<sample>
class MetaXClass(type):
    def Method1(self): pass
class Xclass(object):
    __metaclass__ = MetaXClass
    @classmethod
    def Method2(self): pass
</sample>

> You can drop the 'global' - there's nothing like a real global scope in
> Python.


Yes, they are. Functions defined at module level or using staticmethod
decorator don't receive the instance as argument, they are globa,
You can study in Python:
 * global keyword
 * globals function

> Nope. Functions wrapped by a method object receive the instance as *first*
> (not 'special') argument. In Python, a method is only a wrapper object
> around the function, the class and the instance. This wrapper is built by
> the function object's __get__ method (descriptor protocol) each time the
> attribute is looked up.

Seriously, I'm a programmer, not a intermediate code engineer. I know
very well how python work in its inner, but this forum is to talk
about Python programming.
Nevertheless, in some level is always call the original defined
function, that YES, it receives the self as an argument.

> Why "theoretically speaking" ?

Why not?

>>>> isinstance(Foo, object)
> True

That's only means that python is nice because fulfills very well the
best OOP theory.

> <ot>
> pep08 : method names should be all_lower
> </ot>

Not for me. I use to be consistent in being pythonic, but there are
some few exceptions.

> <ot>
> The convention for classmethod is to name the first function argument 'cls'.
> </ot>

I just wanted the guy who made the question, don't see other
differences but classmethod decorator.
Sorry!

> Nope. There's nothing like "coercion" in Python.

http://docs.python.org/ref/coercion-rules.html

> If you really intend to go into low-level explanations of Python's object


I NEVER pretended to do that. Programming I like is high level,
because of that Python is right now my preferred language,
nevertheless I know every thing I'm interested in inner Python.

My best regards



More information about the Python-list mailing list