Friday, November 5, 2010

Python instance methods - How are they different from ordinary functions?

Python OO model works exceptionally well. Its introspection capabilities, and flexibility provided by the descriptors implementation allow for a lot of interesting thing - one of the nicest being what is available through the "property" built-in call.

However, the descriptor model implies in some behaviors that can raise eyebrows, even for experienced users. Like this bit here:
>>> class A(object):
...   def b(self): pass
...
>>> A.b is A.b
False
>>> a = A()
>>> a.b is a.b
False



Eeeeek!  :-) And now what? Well - it happens that while Object methods are not just functions that receive the object instance as its first parameter - that is, methods are objects from a different class than functions - they are not recorded in the class or in the object instance itself. Rather: both bound and unbound methods for new-style classes in Python are created at attribute retrieval-time: The very "a.b" expression above is, internally, a call to a.__getattribute__("b") - and this call uses the descritor mechanism to wrap the function stored in the class __dict__ attribute in an instance method.

So, methods are objects that contain a reference to the original function, but prepends the "self" variable to the function call when they are called for a bound instance:

   
>>> class A(object):
...   def b(self): pass
>>> a = A()
>>> a.b.im_func
<function b at 0x7f6613a6cd70>
>>> a.b.im_func is A.b.im_func is A.__dict__["b"]
True


(extra bonus: I just learnd about the possibility of concatenating the "is" operator here as well :-) )

This way of doing things works fine, in almost all circunstances. However, the only kinds of objects that are turned into "properties which are method factories" at class creation time are functions. If you want to have other kind of callable objects (like any instance of a class with a "call") into a  proper method, you have to create the descriptor for that yourself. It is actually quite simple -you just have to "implement the descriptor protocol" - which is another way to say, you just have to add a __get__ method :-) . Let's suppose I want a method for a class that is itself an object which keeps track of how many times it was called:
   
class CounterFunction(object):
    """Designed to work as function decorator - will not work for methods """
    def __init__(self, function):
        self.func = function
        self.counter = 0
    def __call__(self, *args, **kw):
        self.counter += 1
        return self.func(*args, **kw)

@CounterFunction
def b(): pass

b()
b()
print b.counter
#-> 2


And if we try to use that as a decorator for a method,
the method will no longer "work":

class C(object):
    @CounterFunction
    def d(self): pass

c = C()
c.d()
#Traceback (most recent call last):
#  File "<stdin>", line 1, in <module>
#  File "<stdin>", line 8, in __call__
#TypeError: d() takes exactly 1 argument (0 given)


However a call like "c.d(c) " would succeed  - the c.d object just does not get its instance prepended to the argument list.

If we do, instead:
from types import MethodType

class CounterFunction(object):
    """Designed to work as function or method decorator """
    def __init__(self, function):
        self.func = function
        self.counter = 0
    def __call__(self, *args, **kw):
        self.counter += 1
        return self.func(*args, **kw)
    def __get__(self, instance, owner):
        return MethodType(self, instance, owner)

class C(object):
    @CounterFunction
    def d(self): pass

c = C()
c.d()
print c.d.counter
#-> 1

This "__get__" method, as above, does exactly what the authomatically created "getter" for methods does when we use an ordnary method. This behavior is implemented in the metaclass for Python classes, that is, the "type" method. Whena  class object is instantiaded (the moment it is defined) the __new__ method inside type checks which objects in its body dictionary are functions, and replaces them by the descritors with a __get__ method like the one above.

It should be noted that it is an official advise that you can increase performance in certain code patterns by caching an object method in a local variable rather than retrieveing the method each time you want to call it - that is:

for i in xrange(5000):
    a.do_something()


is actually faster as:

b = a.do_something
for i in xrange(5000):
    b()


The exact behavior of methods and descriptors is described in detail here in the official documentation, here, --look at "user methods"  objects:
http://docs.python.org/reference/datamodel.html

78 comments:

  1. Decorator parece ser bem mais tranqüilo que as annotations do Java.

    ReplyDelete
  2. Thanks for the post, it solved a problem I was having and I've posted it as a solution to a StackOverflow question, too.

    http://stackoverflow.com/questions/9523370/adding-attributes-to-instance-methods-in-python/

    ReplyDelete
  3. It has been simply incredibly generous with you to provide openly what exactly many individuals would’ve marketed for an eBook to end up making some cash for their end, primarily given that you could have tried it in the event you wanted.

    python training in bangalore|

    ReplyDelete
  4. This is the exact information I am been searching for, Thanks for sharing the required infos with the clear update and required pointsapple service center chennai
    apple service center in chennai

    ReplyDelete
  5. Thanks for giving great kind of information. So useful and practical for me. Thanks for your excellent blog, nice work keep it up thanks for sharing the knowledge.
    moto service center
    motorola service center
    motorola service center near me

    ReplyDelete
  6. I believe that your blog will surely help the readers who are really in need of this vital piece of information. Waiting for your updates.
    lg mobile service center in chennai
    lg mobile service center
    lg mobile service chennai

    ReplyDelete
  7. Attend The Python training in bangalore From ExcelR. Practical Python training in bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Python training in bangalore.
    python training in bangalore

    ReplyDelete
  8. I have to search sites with relevant information on given topic and provide them to teacher our opinion and the article.
    data analytics course mumbai

    data science interview questions

    business analytics course

    ReplyDelete
  9. I finally found great post here.I will get back here. I just added your blog to my bookmark sites. thanks.Quality posts is the crucial to invite the visitors to visit the web page, that's what this web page is providing.
    ExcelR Data Science course in Mumbai
    ExcelR Courses in data Analytics
    data science interview questions
    ExcelR Business Analytics courses in Mumbai

    ReplyDelete
  10. Machine learning course is structured to impart machine learning skills using the two most popular programming languages Python and R. This course enables the student to perform Data Wrangling, Data Cleansing and Data Mining (Supervised and Unsupervised) on structured and unstructured data. Python and R can be used as statistical software to develop regression analysis algorithms and other statistical computations in machine learning. Apprehend different machine learning algorithms like Black Box techniques, Neural Networks and Support Vector Machines. The student can build prediction models with Amazon Machine Learning Services in the best machine learning course in Hyderabad. Present reports to management with Data Visualization software Tableau.

    For more info click the below link 360digitmg machine-learning

    ReplyDelete

  11. Hey, thanks for this great article I really like this post and I love your blog and also Check IOT training in hyderabad at 360DIGITMG.
    360Digitmg IOT training in hyderabad

    ReplyDelete
  12. Excellent Blog! I would like to thank for the efforts you have made in writing this post. I am hoping the same best work from you in the future as well. I wanted to thank you for this websites! Thanks for sharing. Great websites!

    360 Digitmg Data-science training in Chennai

    ReplyDelete
  13. Good Article .we are sharing a such wonderful information
    AWS Training In Hyderabad

    ReplyDelete
  14. This comment has been removed by the author.

    ReplyDelete
  15. This is a wonderful article, Given so much info in it, These type of articles keeps the users interest in the website, and keep on sharing more ... good luck.

    Correlation vs Covariance

    ReplyDelete
  16. There are many things mentioned in this article I would not have thought of on my own. This material is inspirational, interesting and it allows the readers to open up their minds to original thinking.
    SAP training in Kolkata
    Best SAP training in Kolkata
    SAP training institute in Kolkata

    ReplyDelete
  17. If I had to give a prime example of great quality content, this article would be one. It's well-written material that keeps your interest well.
    SAP training in Mumbai
    Best SAP training in Mumbai
    SAP training institute Mumbai

    ReplyDelete
  18. wonderful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article resolved my all queries.
    data science interview questions

    ReplyDelete
  19. A good blog always comes-up with new and exciting information and while reading I have feel that this blog is really have all those quality that qualify a blog to be a one.
    data science courses

    ReplyDelete
  20. Your site is truly cool and this is an extraordinary moving article.
    business analytics course

    ReplyDelete
  21. Thanks for posting the best information and the blog is very interested.python course in Bangalore

    ReplyDelete
  22. Wow such an amazing content keep it up. I have bookmarked your page to check out more informative content here.

    SASVBA Delhi provides extensive MERN STACK COURSE IN DELHI The extensive hands-on session, hosted by the MERN Stack Training Institute in Delhi, includes live projects and simulations.

    FOR MORE INFO:

    ReplyDelete
  23. First You got a great blog .I will be interested in more similar topics. I see you have really very useful topics, I will be always checking your blog thanks.
    business analytics course

    ReplyDelete
  24. This comment has been removed by the author.

    ReplyDelete
  25. great post ! This was actually what i was looking for and i am glad to came here!
    F5 Load balancer Training

    ReplyDelete
  26. A great website with interesting and unique material what else would you need.
    best data science institute in hyderabad

    ReplyDelete
  27. First You got a great blog .I will be interested in more similar topics. i see you got really very useful topics, i will be always checking your blog thanks.
    business analytics course

    ReplyDelete
  28. IGRS Telangana portal is the all type services provide in telangana state

    ReplyDelete
  29. KGF 2 Release Date : Directed by Prashanth Neel. With Yash, Sanjay Dutt, Raveena Tandon, Prakash Raj. The blood-soaked land of Kolar Gold Fields

    ReplyDelete
  30. You can now make the most of Python programming as a dominant career by enrolling in the AI Patasala Python Training in Hyderabad.

    ReplyDelete
  31. Python Training at Hyderabad from AI Patasala would be ideal for students who want to develop their technical abilities in Python.
    Python Institute in Hyderabad

    ReplyDelete
  32. Movie4Me is a top movie and leaked movie website

    ReplyDelete
  33. Extremely overall quite fascinating post. I was searching for this sort of data and delighted in perusing this one. Continue posting. A debt of gratitude is in order for sharing. data scientist course in lucknow

    ReplyDelete
  34. Really an awesome blog. Nice information and knowledgeable content. Keep sharing more articles with us. Thank you.
    Data Science Course Training Institute in Hyderabad with Placements
    Data Science Course Training in Hyderabad

    ReplyDelete
  35. Amazingly by and large very interesting post. I was looking for such an information and thoroughly enjoyed examining this one.
    Keep posting. An obligation of appreciation is all together for sharing.
    business analytics course in gwalior


    ReplyDelete
  36. Really an awesome blog and informative content. If you want to become a data scientist, then check out the following link.
    AI Patasala Data Science Training in Hyderabad

    ReplyDelete
  37. Very interesting blog. Alot of blogs I see these days don't really provide anything that I'm interested in, but I'm most definately interested in this one. Just thought that I would post and let you know.
    cyber security course malaysia

    ReplyDelete
  38. I think this is an informative post and it is very useful and knowledgeable. therefore, I would like to thank you for the efforts you have made in writing this article.
    business analytics course in hyderabad

    ReplyDelete
  39. This comment has been removed by the author.

    ReplyDelete
  40. Learn to use analytics tools and techniques to manage and analyze large sets of data from Data Science training institutes in Bangalore. Learn to take on business challenges and solve problems by uncovering valuable insights from data. Learn from the comprehensively designed curriculum by the industry experts and work on live projects to sharpen your skills.

    Data Science Training in Jodhpur

    ReplyDelete
  41. Advance your technical skills required to crack huge datasets to bring out new possibilities from data. Join the Data Science institutes in Bangalore and get access to top industry trainers, LMS, live projects, assignments, and mock interviews to skyrocket your career in the ever- evolving field of Data Science.

    Data Science Course in Bangalore

    ReplyDelete
  42. Advance your technical skills required to crack huge datasets to bring out new possibilities from data. Join the Data Science institutes in Bangalore and get access to top industry trainers, LMS, live projects, assignments, and mock interviews to skyrocket your career in the ever- evolving field of Data Science.

    Data Science in Bangalore

    ReplyDelete
  43. It is truly an honour to run across informational content like the one you have written. You are evidently knowledgeable on this written topic and you have unique views to share.
    SAP training in Kolkata
    SAP course in Kolkata

    ReplyDelete
  44. A great piece of writing! You have an efficient flair for high quality writing. I am highly impressed by your way of writing this content beyond words. I really have a lot of appreciation for your writing. Thank you so much for all your priceless input on this topic.
    SAP training in Kolkata
    SAP course in Kolkata

    ReplyDelete
  45. By reading this article, many things have piled up in my mind which made me to think about it. You have put some high quality and valuable information here that any reader would love to read. I sincerely share many of your views in this article.
    SAP training in Kolkata
    SAP course in Kolkata

    ReplyDelete
  46. I found this post on data science quite interesting and unique, because it is interesting and useful at the same time. I found myself learning new concepts about data science from this post, even though I have been working in this field for some time now. I have re-visited this post many times now, and each time, it unfolds a new meaning to me.

    Kickstart your career by enrolling in this Data Science Certification Course in Chennai

    ReplyDelete
  47. Being a data science aspirant, this post was of great help to me. I came across this article while looking for a path to becoming a certified data scientist looking for an accredited data science course. This site contains an extraordinary collection of material that 360DigiTMG courses.

    Kickstart your career by enrolling in this Data Science Certification Course in Chennai

    ReplyDelete
  48. Insightful look at Python instance methods and the descriptor model! Your explanation clarifies the dynamic nature of methods and their relationship to the descriptor mechanism. It's fascinating how you've demonstrated method creation customisation using the "get" method, allowing for more flexible behavior. Thank you for the useful information! 🐍🔍
    Data Analytics Courses in India

    ReplyDelete

  49. This insightful explanation sheds light on Python instance methods and their distinctions from regular functions. It clarifies the descriptor model, demonstrating how methods are created and utilized, enhancing our understanding of Python's object-oriented nature.
    Data Analytics Courses in Nashik

    ReplyDelete
  50. Hello Blogger,
    The explanation provided here of Python's instance methods and functions, along with descriptors, provides valuable insights for Python programmers. It clarifies subtle differences and usage scenarios effectively. Thank you.
    Is iim skills fake?

    ReplyDelete
  51. This blog post likely delves into the distinctions between Python instance methods and ordinary functions, a fundamental topic for Python developers. Understanding these differences is crucial for effective object-oriented programming in Python. The post is likely a valuable resource for Python enthusiasts, offering insights and explanations on the unique characteristics and use cases of instance methods. A must-read for those looking to enhance their understanding of Python's object-oriented principles and programming practices.
    Data Analytics Courses in Delhi



    ReplyDelete
  52. Excellent concept, extremely unique idea, and wonderful informational stuff of all kinds.
    Data Analytics Courses in Agra

    ReplyDelete
  53. Thank you so much for explaining these Python instance methods. It has expanded my knowledge regarding this topic.
    Visit - Data Analytics Courses in Delhi

    ReplyDelete
  54. I'd like to convey my appreciation for providing such a valuable and enlightening blog.
    Data Analytics Courses in Leeds

    ReplyDelete
  55. The blog post effectively highlights the comparison of Python instance methods and ordinary functions!
    Digital Marketing Courses in Italy

    ReplyDelete
  56. Thank you for sharing in depth knowledge and insights on Python instance methods and ordinary functions.
    Adwords marketing

    ReplyDelete
  57. Excellent information. The concept that was explained is very useful and also ideas are awesome, I really love to read such a wonderful article. Thank you for the information.
    Investment Banking courses in bangalore

    ReplyDelete
  58. Fascinating insight into Python's descriptor model! Your explanation clarifies the mechanics of methods and descriptors. Thank you for sharing!

    Investment Banking Industry

    ReplyDelete
  59. A Dedicated Server requires an operating system that is compatible with the server hardware and the applications or services.

    ReplyDelete