Jump to content
  • Advertisement
Sign in to follow this  
FlaiseSaffron

[PYTHON] Event-driven wrapper

This topic is 4004 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

I'm trying to write a wrapper class for a numbers that will trigger an even each time the value of the number changes, but will otherwise not affect the code that uses the number. For example, I overrode the arithmetic operators so that I can use infix operators:
x = Value(0)

# register functions as listeners to x

x += 42
print x


The above code prints "42", just as though x were an ordinary int. The only difference is that the listener functions are called when the line "x += 42" is executed. The problem is that python doesn't support the assignment operator, so in order to assign an arbitrary value to one of these wrappers, I have to type the following:
x += 1337 - x  # x = 1337 would be nice...


That isn't exactly modular. Is there any way around this? Also, is there any way for the wrapper to pass method calls directly to the underlying value without rewriting the wrapper for each new data type so that it will work with vectors and matricies? EDIT: Fixed typo. [Edited by - Dathgale on November 2, 2007 8:08:01 PM]

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Dathgale
That isn't exactly modular. Is there any way around this?

No. Python doesn't support the concept of assignment; the = symbol performs association of an object with a symbolic reference variable. Using it on an already bound variable rebinds it to a new object. (This, incidentally, ties into why all value types in Python are immutable.)

Quote:
Also, is there any way for the wrapper to pass method calls directly to the underlying value without rewriting the wrapper for each new data type so that it will work with vectors and matricies?

Yes, but to tell you how I'd need to see which method calls you're talking about, so I don't confuse you. You're going to want to look at getattr and setattr, in all likelihood.

Share this post


Link to post
Share on other sites
An example would be:


v = Value(Vector(1, 2, 3))

result = v.dot(Vector(4, 5, 6))




I want the Value object to pass the .dot() method call to the underlying Vector. Basically like this:


class Value:
...

def dot(other):
return target.dot(other)




...except without adding a new method to the Value class to accomodate every possible target.

Share this post


Link to post
Share on other sites
Untested code:


class Value:
# other methods here....

# when Value is queried for an attribute member that wasn't found,
# whether a value or a callable (eg. a function), ask
# self.target for it instead
def __getattr__(self, attr):
return getattr(self.target, attr)
# probably __setattr__ version here too...


For more detail, look here, and note potential pitfalls of this approach.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!