S
Simon Bunker
Hi I have code similar to this:
class Input(object):
def __init__(self, val):
self.value = val
def __get__(self, obj, objtype):
return self.value
def __set__(self, obj, val):
# do some checking... only accept floats etc
self.value = val
class Node(object):
a = Input(1)
b = Input(2)
I realise that a and b are now class attributes - however I want to do this:
node1 = Node()
node2 = Node()
node1.a = 3
node.b = 4
And have them keep these values per instance. However now node1.a is 4
when it should be 3.
Basically I want to have the Input class as a gateway that does lots of
checking when the attibute is assigned or read.
I have had a look at __getattribute__(), but this gets very ugly as I
have to check if the attribute is an Input class or not.
Also I don't think property() is appropriate is it? All of the
attributes will essentially be doing the same thing - they should not
have individual set/get commands.
Is there any way of doing this nicely in Python?
thanks
Simon
class Input(object):
def __init__(self, val):
self.value = val
def __get__(self, obj, objtype):
return self.value
def __set__(self, obj, val):
# do some checking... only accept floats etc
self.value = val
class Node(object):
a = Input(1)
b = Input(2)
I realise that a and b are now class attributes - however I want to do this:
node1 = Node()
node2 = Node()
node1.a = 3
node.b = 4
And have them keep these values per instance. However now node1.a is 4
when it should be 3.
Basically I want to have the Input class as a gateway that does lots of
checking when the attibute is assigned or read.
I have had a look at __getattribute__(), but this gets very ugly as I
have to check if the attribute is an Input class or not.
Also I don't think property() is appropriate is it? All of the
attributes will essentially be doing the same thing - they should not
have individual set/get commands.
Is there any way of doing this nicely in Python?
thanks
Simon