473,805 Members | 1,981 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1257
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,d efault,name):
self.default = default
self.name = name # or, create a name automatically
def __get__(self,ob j,objtype):
return getattr(obj,sel f.name,self.def ault)
def __set__(self,ob j,value):
setattr(obj,sel f.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***@renderman ia.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***@renderman ia.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
attribute s 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,d efault,name):
self.default = default
self.name = name # or, create a name automatically
def __get__(self,ob j,objtype):
return getattr(obj,sel f.name,self.def ault)
def __set__(self,ob j,value):
setattr(obj,sel f.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
1813
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 descriptor types, like: member descriptors method descriptors getset descriptors wrapper descriptors
14
1649
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 object stood on itself instead of being an attribute to an other object. So what are the reasons for limiting this feature in such a way? -- Antoon Pardon
10
5116
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 Parent.__init__()"
12
1452
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, objtype=None): if obj is None: return self return getattr(obj, 'bar', 'no bar')
0
1385
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 file he wants - are you trying to trap any file operation it may want to do? You *could* do such things -google for "code injection" and "API hooking"- but I doubt it's what you really want.
9
1638
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 string, or integer) that can interact with other datatypes and be passed as an argument? The following code of course gives an error:
28
2193
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 that it simplifies the simulator very much. In this translation I want to keep the JAL-syntax as much as possible intact, so anyone who can read JAL, can also understand the Python syntax. One of the problems is the alias statement, assigning a...
7
1506
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 really confused with OO sometimes), like the one in this example: class Foo(): self.a = "bar" self.z = "test" self.var = 2
2
5444
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 are reading the content written into the file. Can anybody tell me is there a performance or any other gain (except for the multex locking) by using different file descriptors in each thread for the same file rather than using a single FD with a...
0
9716
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9596
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10609
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10105
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7646
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5677
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4323
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3845
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.