7

If I have a class with several functions:

class Example:

    def func1(self):
        print 'Hi1'
    def func2(self):
        print 'Hi2'
    def func3(self):
        print 'Hi3'

If I create several instances of 'Example', does each instance store its own copies of the functions in the class? Or does Python have a smart way to store the definition only once and look it up every time an instance uses a function in the class?

Also, what about static functions? Does the class keep only one copy of each static function?

Squall Leohart
  • 657
  • 2
  • 8
  • 20

2 Answers2

15

When instantiating a class, no new function objects are created, neither for instance methods nor for static methods. When accessing an instance method via obj.func1, a new wrapper object called a "bound method" is created, which will be only kept as long as needed. The wrapper object is ligh-weight and contains basically a pointer to the underlying function object and the instance (which is passed as self parameter when then function is called).

Note that using staticmethod is almost always a mistake in Python. It owes its existence to a historical mistake. You usually want a module-level function if you think you need a static method.

Sven Marnach
  • 574,206
  • 118
  • 941
  • 841
  • Nicely put and understandable - good note about staticmethod's as well – Jon Clements Aug 13 '12 at 17:53
  • Also note that functions objects themselves can (and do) share bytecode, and IIRC only add a bit of metadata (name, module, docstring, captured variables, etc.) atop of that. –  Aug 13 '12 at 18:13
  • @delnan: That's only true for *local* function, i.e. functions defined inside functions. The class body is executed only once, and for the functions in the class body, there is a one-to-one correspondence between function objects and code objects. – Sven Marnach Aug 13 '12 at 18:26
  • @SvenMarnach Really? On Python 3.2 with `def f() { def g(): pass; return g }`, I get true for `f().__code__ is f().__code__`. And I don't see why it wouldn't be the case in 2.x, as code objects have apparently been immutable for a very long time. –  Aug 13 '12 at 18:38
  • @delnan: I don't see any contradiction between your observation (which is also valid for Python 2.x) and my comment. – Sven Marnach Aug 14 '12 at 10:21
  • @SvenMarnach Nevermind, I misread. Your comment still doesn't make sense to me. Most of the time, methods and their enclosing classes are only defined once, so *of course* there is nothing to share. However, there is nothing special about function definitions in classes, or class definitions for that matter. You can return local functions for use as a method, and you can put class definitions inside functions so they are executed on every call (a sometimes quite readable alternative to the 3-argument `type`). And because there is nothing special about them, bytecode sharing applies too. –  Aug 14 '12 at 10:29
0

The functions are "stored" in the class, both static and non-static.

Qiau
  • 5,976
  • 3
  • 29
  • 40