Skip to content
Advertisement

Can a class instance return tuple without using attribute?

I’ve got a small problem which in itself isn’t a big deal but is bugging me none the less. Consider the code below.

JavaScript

When passing vector as a parameter to pygame.Surface I have to use the ‘t’ attribute to access the tuple stored in ‘size’.

Is there a way to have it simply like this?

JavaScript

Is there a magic method that returns a value if no attribute or method is referenced?

In pygame you can define a vector like this, v = pygame.math.vector2(10, 10) and then pass that vector to a surfaces blit function like this, someSurface.blit(sprite, v) and there’s no need to reference a method or attribute to get the value so it does seem possible, though I guess it’s perfectly plausible that the blit function can handle a vector2 as an argument?

Thank you for you time.

Advertisement

Answer

Implement __len__ and __getitem__:

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement