Jump to content
  • Advertisement
Sign in to follow this  
Zaris

python and tuple

This topic is 3611 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is there a way I can get my own class to return a tuple to be use in a function automaticly. ex. x = myPoint(3,4) screen.blit(picture,mypoint) This works, but how do I remove the need to call the tuple function

screen.blit(picture, tuple(mypoint))


[Edited by - Zaris on July 4, 2008 12:56:30 PM]

Share this post


Link to post
Share on other sites
Advertisement
Why do you need a class for points when a tuple is already a nearly ideal container for them? You're going to need to call some function to convert between the class and the tuple


class MyPoint:
def __init__(new_x, new_y):
x = new_x
y = new_y
def getTuple():
return (x, y)

pos = MyPoint(4, 3)

screen.blit(picture, pos.getTuple())



seriously, though...

pos = (4, 3)

screen.blit(picture, pos)



Any functionality that you might get by methods of a point class can be done with free functions.

Share this post


Link to post
Share on other sites
I'm trying to create a point class that will handled internally basic actions needed by a point or vector in a game. I want to be able to override the *,+, - operators and have a type conversion handled automaticly without an explict call to the class function.

Share this post


Link to post
Share on other sites
I found a solution


 class Point(object):


def __init__(self, x, y):
self.x = x
self.y = y

def __getitem__(self, index):
li = [self.x,self.y]
try:
return li[index]
except IndexError:
raise IndexError, "incorrect arg"

def __len__(self):

return 2




I just needed to set up a function to return the length of the object.

Share this post


Link to post
Share on other sites
Yeah, you don't actually need to return a tuple, you merely need for your type to behave like a tuple. You'll find this often in Python, where constructs don't depend on explicit type information, but rather on interfaces and conformance to a concept.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!