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__' 3 1095
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__(t ype):
_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_sh ared, set_shared)
def get_shared(self ):
return self.__class__. shared
def set_shared(self , value):
self.__class__. shared = value
shared = property(get_sh ared, set_shared)
Does that what you want?
Peter
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
"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__(t ype):
_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_sh ared, set_shared)
def get_shared(self ):
return self.__class__. shared
def set_shared(self , value):
self.__class__. shared = value
shared = property(get_sh ared, 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__,n m, o )
class A:
__metaclass__ = Ameta
class B( A ):
A.createShared( "cls2",1)
Many thanks,
gary This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 __get__(self, obj, type=None):
print "in get method"
|
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 the error message:
|
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 object they are
wrapping, whereas the descriptors written in C do not.
For example, taking the code from
http://users.rcn.com/python/download/Descriptor.htm which implements what
one would think is a straightforward implementation:
class...
|
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,
for this one particular problem I am at a loss and am hoping
someone on the list can provide me with a suggestion on how to
address the issue.
The basic issue is the manipulation of flags on the file
|
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
| |
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 stuff>" < "\r\n"
How is this possible with the standard header files?
I need this because my objects have xml'ed << operators, but now I can't
figure out how to use them on the actual network stream.
|
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?
__________________________________________________
|
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 auto-generated e-mails are getting "deferred" by AOL
now with an error:
Deferred: Bad file descriptor
I can't find anything on their support site about this, nor Googling.
Any ideas?
|
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 handle/reference/
pointer to an attribute. I know I could make my own class to do this
(using the __dict__ of the object and the attribute name), but I would
like to use something standard (at least a protocol) if it exists.
All that is needed in the protocol...
|
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...
|
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...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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
| |
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...
| |