0

I've been reading about slots, and since this seems to be a serious memory issue that's somewhat different from C++ and Java that I'm familiar with, I just wanted to know the right way to instantiate classes of other libraries in my class.
Example: This is how I'm currently doing it (roughly):

import pymunk
from pymunk import Vec2d
class RobotBody(object):
    chassisXY = Vec2d(0, 0)
    chassisMass = 10
    chassis_b = []  # chassis body
    chassis_s = []  # chassis shape

    def __init__(self, chassisCenterPosition):
        self.chassisXY = chassisCenterPosition
        self.chassis_b = pymunk.Body(self.chassisMass, pymunk.moment_for_box(self.chassisMass, (100, 100)))
        self.chassis_b.position = self.chassisXY
        self.chassis_shape = pymunk.Poly.create_box(self.chassis_b, (100, 100))

rb = RobotBody(Vec2d(600, 100))

However, is the right way to instantiate chassis_b and chassis_s like this?

chassis_b = pymunk.Body()  # chassis body
chassis_s = pymunk.Poly()  # chassis shape

The member variables are going to get replaced by other Body and Poly objects during __init__ anyway, so is it worth instantiating them with a class instance or is there a way to instantiate it as null, which would hopefully save memory/processing-time? I tried using pass, but it didn't seem to work in this case. I have the same question for Vec2d or any other 3rd party library class that is used.

It'd also be nice if I could have some info/pointers on how this memory management happens in Python.

UPDATE: I just found that it isn't possible to instantiate chassis_b = pymunk.Body() because Body() needs 3 parameters passed to it. Still, my question remains the same.

foobarna
  • 854
  • 5
  • 16
Nav
  • 19,885
  • 27
  • 92
  • 135

1 Answers1

1

If you really want to declare class attributes, then you can use None (to not spend time on instantiating).

class RobotBody(object):
    chassisXY = None
    chassisMass = 10
    chassis_b = None  # chassis body
    chassis_s = None  # chassis shape

But, in your particular case, you don't really need the chassisXY, chassis_b and chassis_s as class attributes, because you always instantiate them in the RobotBody.__init__. So, to better perform in memory/time you can remove them.

In Python, None is your null.

foobarna
  • 854
  • 5
  • 16
  • Why is it not necessary to instantiate those class attributes? Is it a pythonic way of doing things? I kept it there for maintainability. Later when I look at the code, I want to know which member variables are class attributes, without having to look at `__init__`. Maybe I'm wrong. I'm just accustomed to it. Do let me know if the Python way is better and why, in terms of memory/computation. – Nav Mar 30 '19 at 12:54
  • In general, putting them in `__init__` is the way to do it. There are use cases when it's used as class attributes, if you use a framework for example. See https://docs.djangoproject.com/en/2.1/topics/db/models/#quick-example for example. As memory, you end up occupying in extra of what you declare. In computation, if you have `chassisMass = something_heavy()` then every time you import that module it will execute that function. – foobarna Mar 30 '19 at 13:08
  • Turns out this is also a reason (class variables get shared across instances): https://stackoverflow.com/questions/1680528/how-to-avoid-having-class-data-shared-among-instances. Rather annoying; the way Python is designed. – Nav Apr 13 '19 at 10:38