@classmethod decorator concept

42 views
Skip to first unread message

rudih...@gmail.com

unread,
Oct 29, 2015, 2:41:58 PM10/29/15
to Python Programming for Autodesk Maya
hi,
I understand that a @classmethod decorator allows to access a method without creating the object. For instance
class myCLass(object):

def myMethod(self):
print "hello"

myCLass().myMethod()
myCLass.myMethod() #<< that would give an error because the object is requiered

to do so I create a @classmethod

class myCLass(object):

@classmethod
def myMethod(self):
print "hello"

myCLass.myMethod() # now it works


my question is. What is the advantage? does that make evaluation faster?
I ask all that because I am learning maya api and in an example I got :

mSelectionList = OpenMaya.MSelectionList()
OpenMaya.MGlobal.getActiveSelectionList(mSelectionList)

I am confused because I was able to use the method getActiveSelectionList() without creating the object MGlobal. I would have wrote
mGlobal = OpenMaya.MGlogal
mGlobal.getActiveSelectionList(mSelectionList)

it is like the method getActiveSelectionList() had somehow @classmethod

do I make any sence?
thans

Justin Israel

unread,
Oct 29, 2015, 3:01:20 PM10/29/15
to Python Programming for Autodesk Maya


On Fri, 30 Oct 2015 7:41 AM  <rudih...@gmail.com> wrote:

hi,
I understand that a @classmethod decorator allows to access a method without creating the object. For instance
class myCLass(object):

    def myMethod(self):
        print "hello"

myCLass().myMethod()
myCLass.myMethod() #<< that would give an error because the object is requiered

to do so I create a @classmethod

class myCLass(object):

    @classmethod
    def myMethod(self):
        print "hello"

myCLass.myMethod() # now it works

my question is. What is the advantage? does that make evaluation faster?

It's not a performance thing. It's about organising what a function does and how it is related to a namespace / class


I ask all that because I am learning maya api and in an example I got :

mSelectionList = OpenMaya.MSelectionList()
OpenMaya.MGlobal.getActiveSelectionList(mSelectionList)

I am confused because I was able to use the method getActiveSelectionList() without creating the object MGlobal. I would have wrote
mGlobal = OpenMaya.MGlogal
mGlobal.getActiveSelectionList(mSelectionList)

it is like the method getActiveSelectionList() had somehow @classmethod

If you look at the docs for MGlobal, you will see that everything is a static function :
http://docs.autodesk.com/MAYAUL/2014/ENU/Maya-API-Documentation/cpp_ref/class_m_global.html#a6d81d38246555884897fb153c93aaf42

A static function is effectively a normal function that sits on the class and can be called without an instance. Python has @staticmethod as well, and it's basically the same as @classmethod except you don't even get passed a class object.

A classmethod has a use case when you want to have custom constructors for a class, or you have logic that is specific to just that class, and wants to be able to call other classmethods and class attributes as well. You can see that in the case of MGlobal, is just groups together a bunch of functions in a namespace.

Many times when you think you need a staticmethod, you can probably also just make it a normal free function in the module. It is only really an organisational tool to group similar behaviours.

do I make any sence?
thans

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/3bdf070b-31db-413f-8e6b-682cb2258f6a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Reply all
Reply to author
Forward
0 new messages