Monday, 15 June 2015

Is it possible to add an attribute to an imported class in python? -



Is it possible to add an attribute to an imported class in python? -

i explain situation question makes more sense.

i programming game using python , pygame. pretty minimalist game without lot of graphics, , want each level grid of either black or white squares. think makes lot more sense have each level defined string of 1's , 0's so

0000 0101 1010 1111

as opposed to:

level1 = (pygame.rect(0, 0, 100, 100), pygame.rect(100, 0, 100, 100), pygame.rect(200, 0, 100, 100), pygame.rect(300, 0, 100, 100), ... ...

and on , forth every single rect. sec way clunky , awkward, , create hard design levels. in python, readability counts, , beautiful improve ugly. =)

i planning on using first way, , having function returns list of pygame.rect objects (similar code above) 0's black tiles, , 1's white tiles. unfortunately though, pygame.rect objects don't contain info color are.

now question two-fold.

1) possible add together attribute existing class (in example, adding attribute rect.color) without going source code of module came , changing yourself? (becuase seems bad idea.)

2) if above possible, wise? or shooting in foot?

understand of course, realize not way accomplish want. if wanted tried , true way, create class so:

class tile: def __init__(self, x, y, width, height, color): self.rect = pygame.rect(x, y, width, height) self.color = color def draw(self): pygame.draw.rect(displaysurface, self.color, self.rect)

this obvious way, , works need fine. but, know whether or not possible. if is, seems useful in situations.

***********************edit**************************

okay, took shiplu.mokadd.im's advice , tried using setattr(), works classes have defined myself, not imported classes. here example:

class pet: def __init__(self, name, species): self.name = name self.species = species fido = pet('fido', 'dog') setattr(fido, 'breed', 'golden retriever') print fido.breed

which outputs "golden retriever" expected. example:

import pygame pygame.init() rect = pygame.rect(0, 10, 20, 30) setattr(rect, 'color', "black") print rect.color

outputs error.

traceback (most recent phone call last): file "game.py", line 7, in <module> setattr(rect, 'color', "black") attributeerror: 'pygame.rect' object has no attribute 'color'

i realize should this:

class coloredrect(rect): def __init__(self, color, ...): self.color = color super(coloredred, self).__init__(self, ...)

but possible add together attribute way i've been trying?

also, hope whoever reviews "possible duplicate" questions, realizes i'm not trying add together method... =)

yes possible add together attribute instances , classes (as object too). utilize setattr.

but why do that? should inherit rect class , create coloredrect

class coloredrect(rect): def __init__(self, color, ...): self.color = color super(coloredred, self).__init__(self, ...)

python class attributes pygame

No comments:

Post a Comment