Simke
Simke

Reputation: 7

What is the difference between __main__ and class '__main__?

I am learning about classmethod. I have looked at this example

class A(object):
    def foo(self,x):
        print (self,x)

    @classmethod
    def class_foo(cls,x):
        print(cls,x)

    @staticmethod
    def static_foo(x):
        print (x)   

a=A()
a.foo('pic')
a.class_foo('pic')

This is output

<__main__.A object at 0x7f413121c080> pic
<class '__main__.A'> pic

What is the practical meaning of this?Implementation?

Upvotes: 0

Views: 109

Answers (1)

kingJulian
kingJulian

Reputation: 6180

Implementation-wise, a classmethod takes the first obligatory cls argument, which is not the case for a staticmethod.

The practical meaning is that you can call a classmethod without having to create a class object first. In code this is perfectly valid:

A.class_foo('pic')

You can read more about this subject on this excellent SO post.

Upvotes: 1

Related Questions