473,320 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

per instance descriptors

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
Dec 7 '06 #1
7 1237
Simon Bunker wrote:
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?
What about __setattr__ ? At least from your example, checking happens
only when you set an attribute. If not, post a more representative
sample of what you're trying to do.

George

Dec 7 '06 #2
At Thursday 7/12/2006 01:58, George Sakkis wrote:
>Simon Bunker wrote:
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?

What about __setattr__ ? At least from your example, checking happens
only when you set an attribute. If not, post a more representative
sample of what you're trying to do.
Or search the Python Cookbook - there are zillions of variants on how
to manage properties (using inner classes, using decorators, using
closures, using ...). Traits (code.enthought.com) may be useful too.
--
Gabriel Genellina
Softlab SRL

__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar
Dec 7 '06 #3

Simon Bunker wrote:
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
node2.b = 4

And have them keep these values per instance. However now node1.a is 4
when it should be 3.
[snip]
Is there any way of doing this nicely in Python?
The easiest way is to store the value in a hidden attribute of the
object. For instance:

class Input(object):
def __init__(self,default,name):
self.default = default
self.name = name # or, create a name automatically
def __get__(self,obj,objtype):
return getattr(obj,self.name,self.default)
def __set__(self,obj,value):
setattr(obj,self.name,value)

class Node(object):
a = Input(1,"_a")
b = Input(2,"_b")
Carl Banks

Dec 7 '06 #4

George Sakkis wrote:
Simon Bunker wrote:
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?

What about __setattr__ ? At least from your example, checking happens
only when you set an attribute. If not, post a more representative
sample of what you're trying to do.

George
Yes, but I am setting it in the Node class aren't I? Wouldn't I need to
define __setattr__() in class Node rather than class Input? I don't
want to do this. Or am I getting confused here?

thanks

Simon

Dec 7 '06 #5
si***@rendermania.com wrote:
George Sakkis wrote:
Simon Bunker wrote:
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?
What about __setattr__ ? At least from your example, checking happens
only when you set an attribute. If not, post a more representative
sample of what you're trying to do.

George

Yes, but I am setting it in the Node class aren't I? Wouldn't I need to
define __setattr__() in class Node rather than class Input? I don't
want to do this. Or am I getting confused here?
Yes, __setattr__ would be defined in Node and Input would go. It seems
to me that the only reason you introduced Input was to implement this
controlled attribute access, and as you see it doesn't work as you want
it to. Why not define Node.__setattr__ ?

George

Dec 7 '06 #6
George Sakkis wrote:
si***@rendermania.com wrote:

>>George Sakkis wrote:
>>>Simon Bunker wrote:
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?

What about __setattr__ ? At least from your example, checking happens
only when you set an attribute. If not, post a more representative
sample of what you're trying to do.

George

Yes, but I am setting it in the Node class aren't I? Wouldn't I need to
define __setattr__() in class Node rather than class Input? I don't
want to do this. Or am I getting confused here?


Yes, __setattr__ would be defined in Node and Input would go. It seems
to me that the only reason you introduced Input was to implement this
controlled attribute access, and as you see it doesn't work as you want
it to. Why not define Node.__setattr__ ?

George
The __setattr__ approach is what I am hoping to avoid.

Having the input class means that I just need to assign a class to an
attribute rather than having to filter each attribute name - really
annoying as there will be several classes similar to Node with different
attributes, but which should be handled in the same way.

Frankly descriptors seems exactly what I want - but having them as class
attributes makes them completely useless!

Simon
Dec 7 '06 #7
Carl Banks wrote:
Simon Bunker wrote:
>>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
node2.b = 4

And have them keep these values per instance. However now node1.a is 4
when it should be 3.

[snip]
>>Is there any way of doing this nicely in Python?


The easiest way is to store the value in a hidden attribute of the
object. For instance:

class Input(object):
def __init__(self,default,name):
self.default = default
self.name = name # or, create a name automatically
def __get__(self,obj,objtype):
return getattr(obj,self.name,self.default)
def __set__(self,obj,value):
setattr(obj,self.name,value)

class Node(object):
a = Input(1,"_a")
b = Input(2,"_b")
Carl Banks
This does seem the easiest way - but also a bit of a hack. Would it work
to assign to instance variables - each the class attribute sets the
instance attribute with the same name? Or would that not work?

Simon
Dec 7 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: François Pinard | last post by:
This question is a bit technical, but hopefully, this list will offer me good hints or nice solutions. Happily enough for me, it often does! :-) I would need to recognise and play with...
14
by: Antoon Pardon | last post by:
Can anyone explain why descriptors only work when they are an attribute to an object or class. I think a lot of interesting things one can do with descriptors would be just as interesting if the...
10
by: John M. Gabriele | last post by:
The following short program fails: ----------------------- code ------------------------ #!/usr/bin/python class Parent( object ): def __init__( self ): self.x = 9 print "Inside...
12
by: bruno at modulix | last post by:
Hi I'm currently playing with some (possibly weird...) code, and I'd have a use for per-instance descriptors, ie (dummy code): class DummyDescriptor(object): def __get__(self, obj,...
0
by: jfigueiras | last post by:
>I have a problem with the module subprocess! As many other programs... I'm not sure what you mean by "non-standard file descriptors". The other program is free to open, read, write, etc any...
9
by: manstey | last post by:
Hi, My question probably reflects my misunderstanding of python objects, but I would still like to know the answer. The question is, is it possible for an instnace to have a value (say a...
28
by: Stef Mientki | last post by:
hello, I'm trying to build a simple functional simulator for JAL (a Pascal-like language for PICs). My first action is to translate the JAL code into Python code. The reason for this approach is...
7
by: JonathanB | last post by:
Ok, I know there has to be a way to do this, but my google-fu fails me (once more). I have a class with instance variables (or should they be called attributes, I'm newish to programming and get...
2
by: DJ Dharme | last post by:
Hi all, I am writing a multi-threaded application in c++ running on solaris. I have a file which is updated by a single thread by appending data into the file and at same time the other threads...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.