Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Metaprogramming in Python - adding an object method

I've got a background in Python (though entirely self-taught, so I might have some bad habits or misconceptions), and I'm trying to learn Ruby to broaden my scope.

I was reading through some comparisons, and saw a lot of assertions that "Python can't do metaprogramming" (or, less inflammatorily, "Python can't do metaprogramming so simply as Ruby"). So I went away and quickly read about metaprogramming, and came away with the impression that it is, basically, editing the methods/behaviour of your classes/objects during runtime (please do correct me if I'm incorrect!).

I was under the impression that, since Python is dynamic, that shouldn't be a problem. However, I ran the following test code, which didn't give the response I expected:

>>> class foo:
...     def make_hello_method(self):
...             def hello(obj):
...                     print 'hello'
...             self.hello = hello
...
>>> f = foo()
>>> f.hello()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: foo instance has no attribute 'hello'
>>> f.make_hello_method()
>>> f.hello()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: hello() takes exactly 1 argument (0 given)

I was under the impression that every method of an object was automatically passed the object itself as the first argument (hence the constant requirements to define object methods as (self, [...])). How come f isn't being passed to hello()?

like image 679
scubbo Avatar asked Apr 05 '14 19:04

scubbo


People also ask

What is metaprogramming in Python?

The term metaprogramming refers to the potential for a program to have knowledge of or manipulate itself. Python supports a form of metaprogramming for classes called metaclasses. Metaclasses are an esoteric OOP concept, lurking behind virtually all Python code.

What is __ new __ in Python?

The __new__() is a static method of the object class. It has the following signature: object.__new__(class, *args, **kwargs) Code language: Python (python) The first argument of the __new__ method is the class of the new object that you want to create.

How do you create a metaclass in Python?

To create your own metaclass in Python you really just want to subclass type . A metaclass is most commonly used as a class-factory. When you create an object by calling the class, Python creates a new class (when it executes the 'class' statement) by calling the metaclass.

What does __ call __ do in Python?

In Python, __call__() is used to resolve the code associated with a callable object. Any object can be converted to a callable object just by writing it in a function call format. An object of that kind invokes the __call__() method and executes the code associated with it.


1 Answers

You need to make it an instancemethod. There's a class to do that in the types module. This will work:

self.hello = types.MethodType(hello, self)
like image 133
Daniel Roseman Avatar answered Oct 20 '22 16:10

Daniel Roseman