473,408 Members | 1,729 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,408 software developers and data experts.

descriptor problems

I want to define a class-level attribute that will be shared by all
subclasses. That is, I want this class, every subclass of this class, every
instance of this class and every instance of every subclass to be able to
read and write to the _same_ slot/variable. But I can't figure out how to
do it.

My attempts so far have led me to using a descriptor as follow, but it
doesn't get me where I want to be:

class sharedClassVar(object):
def __init__(self,x=None):
print "creating"
self.value = x
def __get__(self,ob,cls):
print "getting"
return self.value
def __set__(self,ob,x):
print "setting"
self.value = x
class A( object ):
# install it like this I guess:
cls2 = sharedClassVar(5)

class B( A ):
pass

print A.cls2 # so far so good
o = A()
o2 = B()
print o.cls2 # ok
print o2.cls2 # ok
o.cls2 = 2
print B.cls2 # perfect
print o2.cls2 # ""
o2.cls2 = 3
print A.cls2 # ""
print o.cls2 # ""

# but I need to be able to update the value through the class
# how do I do it?
A.cls2 = 4 # whoops - we just overwrote the descriptor with an integer

A.cls2.__set__(None,4) # AttributeError: 'int' object has no attribute
'__set__'


Sep 14 '06 #1
3 1084
Gary Stephenson wrote:
I want to define a class-level attribute that will be shared by all
subclasses. That is, I want this class, every subclass of this class,
every instance of this class and every instance of every subclass to be
able to
read and write to the _same_ slot/variable. But I can't figure out how to
do it.
class A:
class __metaclass__(type):
_shared = 42
def get_shared(self):
return self.__class__._shared
def set_shared(self, value):
print "%r --%r" % (self._shared, value)
self.__class__._shared = value
shared = property(get_shared, set_shared)

def get_shared(self):
return self.__class__.shared
def set_shared(self, value):
self.__class__.shared = value
shared = property(get_shared, set_shared)

Does that what you want?

Peter

Sep 14 '06 #2
Gary Stephenson wrote:
# but I need to be able to update the value through the class
# how do I do it?
A.cls2 = 4 # whoops - we just overwrote the descriptor with an integer

A.cls2.__set__(None,4) # AttributeError: 'int' object has no attribute
'__set__'
You need to define the descriptor at the metaclass level too.

Michele Simionato

Sep 14 '06 #3

"Peter Otten" <__*******@web.dewrote in message
news:ee*************@news.t-online.com...
Gary Stephenson wrote:
>I want to define a class-level attribute that will be shared by all
subclasses. That is, I want this class, every subclass of this class,
every instance of this class and every instance of every subclass to be
able to
read and write to the _same_ slot/variable. But I can't figure out how
to
do it.

class A:
class __metaclass__(type):
_shared = 42
def get_shared(self):
return self.__class__._shared
def set_shared(self, value):
print "%r --%r" % (self._shared, value)
self.__class__._shared = value
shared = property(get_shared, set_shared)

def get_shared(self):
return self.__class__.shared
def set_shared(self, value):
self.__class__.shared = value
shared = property(get_shared, set_shared)

Does that what you want?
It certainly does! But I need to figure out how to package it up a bit more
elegantly. I'd prefer not to have to define a separate metaclass for each
class if I could avoid it. hmmm ..... The following is what I eventually
came up with, which is exactly what I was after:

class valueDescriptor(object):
def __init__(self,x=None):
self.value = x
def __get__(self,ob,cls=None):
return self.value
def __set__(self,ob,x):
self.value = x

class Ameta(type):
def createShared( cls, nm, initValue=None ):
o = valueDescriptor(initValue)
setattr( cls,nm, o )
setattr( cls.__class__,nm, o )

class A:
__metaclass__ = Ameta

class B( A ):
A.createShared("cls2",1)
Many thanks,

gary

Sep 14 '06 #4

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

Similar topics

9
by: John Roth | last post by:
Using Python 2.2.3, I create this script: class AnObject(object): "This might be a descriptor, but so far it doesn't seem like it" def __init__(self, somedata): self.it = somedata def...
9
by: wordsender | last post by:
Hey guys, I can't figure this one out, why is this simple script giving me problems? logfile=file(r'test.txt','w') logfile.write('datetime') test=logfile.readlines() When I run it I get...
0
by: Eric Huss | last post by:
I'm trying to write a descriptor (similar to the EiffelDescriptor example in the Demo directory). However, I noticed an odd behavior that descriptors written in pure Python mask the underlying...
1
by: Robert Schweikert | last post by:
Hi, I am trying to get uses of iostream up to par with the new iostream model, i.e. standard C++ I/O streams. I have run into some problems and was able to work around most of them. However,...
8
by: Joe | last post by:
Hi, I want to use the C library function mkstemp. It returns a unix file descriptor (int) to an open file. How do I convert the file descriptor to a valid ofstream object? Thanks, Joe
3
by: gipsy boy | last post by:
Given a file descriptor (from a network socket, for instance), I want to have an iostream that reads/writes to it, so I can push and read data in the traditional way : sockStream << "<some...
4
by: CSN | last post by:
At startup this appears in the log: WARNING: dup(0) failed after 3195 successes: Bad file descriptor What does it mean? __________________________________________________
7
by: news | last post by:
Recently our mail from our e-commerce site has been rejected by AOL due to an IP block because someone was using our PHP scripts to send spam. Well, I got that fixed. But our legitimate...
3
by: Eric Mahurin | last post by:
Is there a standard way to get a descriptor object for an arbitrary object attribute - independent of whether it uses the descriptor/ property protocol or not. I want some kind of...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.