0

I'm using the pygame library, it has an object pygame.Surface() that has a bunch of methods. This is what I'm trying to do:

my_surface = pygame.Surface((5,5)) #the object requires a size to instantiate
my_surface.coords = (10,10)

But it throws me this error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'pygame.Surface' object has no attribute 'coords'

same things happen if I use

setattr(my_surface, 'coords', (10,10))

instead of

my_surface.coords = (10,10)

BUT if I make my own class

class MyClass():
    pass

instance = MyClass()
instance.coords = (10,10)

it works like a charm.

I've read that it has something to do with not having a __dict__ on every instance of an object (though it was about the default python object()) which would cause objects to use too much memory, and indeed when I do

my_surface.__dict__ #this is a pygame.Surface() object

it throws

AttributeError: 'pygame.Surface' object has no attribute '__dict__'

while

instance.__dict__ #this is a MyClass() object

returns

{}

Is there any way I can modify this particular instance to accept arbitrary attributes? Currently the only workaround I can think of is to create a child class for pygame.Surface() and give it a __dict__, but obviously it's rather confusing to have to choose in advance whether or not I'll need to add arbitrary attributes for every instance of a Surface(), and and instantiating every Surface with a __dict__ defeats the whole purpose of it having no __dict__ in the first place.

1 Answer 1

1

I don't think there's a better way than subclassing Surface if you really need to be able to assign arbitrary attributes to an instance. The subclass can be empty if you don't need to add any other features (the instance __dict__ will be created automatically).

class MySurface(pygame.Surface):
    pass

However, I'm not sure you really need that ability.

A better design is generally to encapsulate the Surface object and whatever other data you have as attributes of some other object type. In Pygame you'll often use a subclass of pygame.sprite.Sprite to bundle a Surface (as the image attribute) with coordinates (as part of the rect attribute). If you have any other data you need to bundle, the Sprite can take it too. Sprite instances have a __dict__ by default, even if you don't write your own subclass.

2
  • Okay I'm fucking stupid, I stumbled upon your solution while researching to explain my problem thoroughly, but then I discovered that the reason objects didn't accept new attributes was the lack of a __dict__ , so I went into the console and tried pygame.Surface().__dict__ , and it returned something, so I thought that since it did have a dict it should accept new attributes. Only after thirty minutes did I realize that it didn't because instances didn't have a __dict__, and at that point I had forgotten I had my answer already so I still posted. Thank you very much nonetheless.
    – Kwantuum
    Commented Sep 8, 2016 at 8:30
  • @Kyll I am terribly sorry but I am unable to find the edit button on the comment, although I see a little pen next to yours that says it was edited so I understand it is possible, I just do not see it. Would you mind sharing a screenshot of where I should look? edit: apparently I can edit this one, maybe I can't edit comments after they've been replied to?
    – Kwantuum
    Commented Sep 8, 2016 at 9:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.