so I was trying to think of ways to reduce python's memory usage when you write your code.

my logic is telling me your class instances will use less memory if you write your class functions before writing your class:

def classfunc( inst, arg ):
    pass

class mainclass: pass

inst = mainclass()
classfunc( inst, 0 )

when you instance mainclass, classfunc will not be defined for each instance.
instead, classfunc would only be defined once for the module.

is my logic correct??

also, is it possible to map the defined function into a class during instantation??
this way the class behaves like it should inst.classfunc(), but doesn't redefine the function.

Recommended Answers

All 3 Replies

Instances don't use less memory if the method is defined outside the class definition. Instances don't keep pointers to the method. They only keep a pointer to the class, which in turns keeps a pointer to the method.

If you want, you can add the function to the class after the class definition

def classfunc( inst, arg ):
    pass

class mainclass: pass

mainclass.classfunc = classfunc

but there is no benefit as compared to writing the function definition directly in the class' body.

Also in python 2 only, write class mainclass(object): pass otherwise it is an old-style class which behaves slightly differently from ordinary classes (they are python 1 classes).

EDIT: The correct way to defined memory-thrifty instances in python is to use the __slots__ member which prevents instances from allocating a dictionary.

but there is no benefit as compared to writing the function definition directly in the class' body.

alright, so my logic was not correct, thanks :)

Also in python 2 only ...

oh alright thanks, I'd noticed minor behavioral differances especially dealing with the instance itself with type comparisons and such, but I didn't realize that was the reason.

just to back you up on that grib, I did some tests over the methods using guppy:

from guppy import hpy

h = hpy()
'''
h.setref()

class test(object):
    def testfunc(): pass

inst = test()
inst2 = test()

print h.heap()
'''

h.setref()

def testfunc(): pass

class test(object): pass

inst = test()
inst2 = test()

print h.heap()
#'''

___

Python 2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>> 
Partition of a set of 39 objects. Total size = 3860 bytes.
 Index  Count   %     Size   % Cumulative  % Kind (class / dict of class)
     0      3   8     1160  30      1160  30 types.FrameType
     1      4  10      560  15      1720  45 dict of guppy.etc.Glue.Interface
     2      1   3      452  12      2172  56 type
     3      3   8      420  11      2592  67 dict (no owner)
     4      4  10      176   5      2768  72 __builtin__.weakref
     5      4  10      144   4      2912  75 str
     6      1   3      140   4      3052  79 dict of guppy.etc.Glue.Owner
     7      1   3      140   4      3192  83 dict of type
     8      4  10      128   3      3320  86 guppy.etc.Glue.Interface
     9      3   8      120   3      3440  89 types.MethodType
<7 more rows. Type e.g. '_.more' to view.>
>>> ================================ RESTART ================================
>>> 
Partition of a set of 39 objects. Total size = 3860 bytes.
 Index  Count   %     Size   % Cumulative  % Kind (class / dict of class)
     0      3   8     1160  30      1160  30 types.FrameType
     1      4  10      560  15      1720  45 dict of guppy.etc.Glue.Interface
     2      1   3      452  12      2172  56 type
     3      3   8      420  11      2592  67 dict (no owner)
     4      4  10      176   5      2768  72 __builtin__.weakref
     5      4  10      144   4      2912  75 str
     6      1   3      140   4      3052  79 dict of guppy.etc.Glue.Owner
     7      1   3      140   4      3192  83 dict of type
     8      4  10      128   3      3320  86 guppy.etc.Glue.Interface
     9      3   8      120   3      3440  89 types.MethodType
<7 more rows. Type e.g. '_.more' to view.>
>>> 

the first is the first test (commented out by the switch).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.