0

I want a certain function callable on a class. Something similar to:

class foo():
    def __init__(self):
        self.img = pygame.Surface((20, 20))
    def magicfunction(self):
        return self.img

bar = foo()

screen = pygame.display.set_mode((200, 200))
screen.blit(bar)

Which magic-function do I have to use?

Pixdigit
  • 80
  • 1
  • 11
  • Your asking for the object to pretend to be a different object when it's referenced? – muddyfish Aug 10 '15 at 10:45
  • That's not possible, unless you mean `bar()` which invokes `__call__`. See [the docs](https://docs.python.org/2/reference/datamodel.html). – jonrsharpe Aug 10 '15 at 10:47
  • 1
    Not possible without some really really really (I mean REALLY) nasty code that examines the code and changes the bytecode whilst running it. Thats overkill for someting your probably only doing because you dont want to type in a few more characters. Deal with it. Sorry :( – muddyfish Aug 10 '15 at 10:52
  • Only one thing you can do: return `self.img` in `__init__` function – Alexey Astahov Aug 10 '15 at 10:55
  • 1
    @AlexeyAstahov you should also read the docs - *"no non-`None` value may be returned by `__init__()`; doing so will cause a `TypeError` to be raised at runtime."* – jonrsharpe Aug 10 '15 at 10:56
  • So you would expect `bar = foo()` to assign the new `foo` instance to `bar`, but `screen.blit(bar)` to call `magicfunction` (really a *method*) and pass the result to `screen.blit`? That's not even logically consistent, and would make your code an unreadable nightmare. – jonrsharpe Aug 10 '15 at 10:57
  • @jonrsharpe I mean: http://stackoverflow.com/questions/16017397/injecting-function-call-after-init-with-decorator – Alexey Astahov Aug 10 '15 at 11:00
  • @AlexeyAstahov how is that related to your previous comment? – jonrsharpe Aug 10 '15 at 11:01
  • @jonrsharpe you can replace standard `__init__` with your implementation of `__init__` and return `self.img` from this function. – Alexey Astahov Aug 10 '15 at 11:03
  • you cant return anything from init – muddyfish Aug 10 '15 at 11:04
  • @jonrsharpe I am fairly new to programming and I knew about `__repr__` so I though there might be a similar function (or as you pointed out it is actually a method). Also `print` calls `__str__` on non-string variables. But thanks for pointing that out. – Pixdigit Aug 10 '15 at 11:05
  • @muddyfish you can. just reimplement standart `__init__` – Alexey Astahov Aug 10 '15 at 11:05
  • 1
    @AlexeyAstahov no, you can't, as I told you above. You can replace `__init__` at runtime, but not with a method that returns anything other than `None`. – jonrsharpe Aug 10 '15 at 11:06
  • @jonrsharpe just read post at link. I can duplicate text here, but I don't think it is useful. – Alexey Astahov Aug 10 '15 at 11:07
  • 1
    The question at the link posted prints out some content, it doesn't return it – muddyfish Aug 10 '15 at 11:08
  • What you propose would be confusing to people reading your code. You _could_ use `__call__` for your magic function and then `screen.blit(bar())` would do what you want, but IMHO it's _much_ better to be explicit and just do `screen.blit(bar.img)`. – PM 2Ring Aug 10 '15 at 11:21
  • Or you could give `foo` a `.blit()` method and do `bar.blit(screen)`, or even just `bar.blit()`, if you've already given `screen` to `bar`, eg during initialisation. – PM 2Ring Aug 10 '15 at 11:27

1 Answers1

0

If I understand you correctly, you want to create a class of your own, which is also a surface. That sounds exactly like inheritance! Try making foo a child of pygame.Surface:

class foo(pygame.Surface):
    def __init__(self):
        pygame.Surface.__init__(self, (20, 20))
        more_data = "You should be able to extend this class freely"
kmaork
  • 5,722
  • 2
  • 23
  • 40