{"id":1053,"date":"2016-07-12T13:13:02","date_gmt":"2016-07-12T13:13:02","guid":{"rendered":"https:\/\/block.arch.ethz.ch\/blog\/?p=1053"},"modified":"2016-07-17T09:13:15","modified_gmt":"2016-07-17T09:13:15","slug":"adding-methods-to-python-classes","status":"publish","type":"post","link":"https:\/\/block.arch.ethz.ch\/blog\/2016\/07\/adding-methods-to-python-classes\/","title":{"rendered":"Adding methods to Python classes"},"content":{"rendered":"<p>The normal way to add functionality (methods) to a class in Python is to define functions in the class body. There are many other ways to accomplish this that can be useful in different situations.<\/p>\n<p><!--more--><\/p>\n<p>This is the traditional way<\/p>\n<pre class=\"brush:py;toolbar:false;gutter:false\">\r\nclass A(object):\r\n    def print_classname(self):\r\n        print self.__class__.__name__\r\n<\/pre>\n<p>The method can also be defined outside the scope of the class. This allows the function &#8220;print_classname&#8221; to be used as a standalone function and as a method of the class.<\/p>\n<pre class=\"brush:py;toolbar:false;gutter:false\">\r\ndef print_classname(a):\r\n    print a.__class__.__name__\r\n\r\nclass A(object):\r\n    print_classname = print_classname\r\n<\/pre>\n<p>Or, equivalently<\/p>\n<pre class=\"brush:py;toolbar:false;gutter:false\">\r\ndef print_classname(a):\r\n    print a.__class__.__name__\r\n\r\nclass A(object):\r\n    pass\r\n\r\nsetattr(A, \"print_classname\", print_classname)\r\n<\/pre>\n<p>Adding the method to an object of type &#8220;A&#8221; is also possible. However, you need to specify that the attribute &#8220;print_classname&#8221; of the object is a method to make sure it will receive a reference to &#8220;self&#8221; as implicit first parameter when it is called. <\/p>\n<pre class=\"brush:py;toolbar:false;gutter:false\">\r\nfrom types import MethodType\r\n\r\ndef print_classname(a):\r\n    print a.__class__.__name__\r\n\r\nclass A(object):\r\n    pass\r\n\r\n# this assigns the method to the instance a, but not to the class definition\r\na = A()\r\na.print_classname = MethodType(print_classname, a, A)\r\n\r\n# this assigns the method to the class definition\r\nA.print_classname = MethodType(print_classname, None, A)\r\n<\/pre>\n<p>Specific methods from another class can also be added (without inherit everything else) by adding the underlying function of the method. Otherwise the method will expect a reference to an instance of the original class as implicit first parameter.<\/p>\n<pre class=\"brush:py;toolbar:false;gutter:false\">\r\nclass B(object):\r\n    def print_classname(self):\r\n        print self.__class__.__name__\r\n\r\n# option 1\r\nclass A(object):\r\n    print_classname = B.print_classname.__func__\r\n\r\n# option 2\r\nclass A(object):\r\n    pass\r\n\r\nsetattr(A, \"print_classname\", B.print_classname.__func__)\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The normal way to add functionality (methods) to a class in Python is to define functions in the class body. There are many other ways to accomplish this that can be useful in different situations.<\/p>\n","protected":false},"author":11,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-1053","post","type-post","status-publish","format-standard","hentry","category-code"],"_links":{"self":[{"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/posts\/1053","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/comments?post=1053"}],"version-history":[{"count":8,"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/posts\/1053\/revisions"}],"predecessor-version":[{"id":1071,"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/posts\/1053\/revisions\/1071"}],"wp:attachment":[{"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/media?parent=1053"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/categories?post=1053"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/tags?post=1053"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}