473,785 Members | 2,640 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Read-only class properties

I'm trying to write a decorator similar to property, with the
difference that it applies to the defining class (and its subclasses)
instead of its instances. This would provide, among others, a way to
define the equivalent of class-level constants:

class Foo(object):
@classproperty
def TheAnswer(cls):
return "The Answer according to %s is 42" % cls.__name__
Foo.TheAnswer
The Answer according to Foo is 42 Foo.TheAnswer = 0

exceptions.Attr ibuteError
....
AttributeError: can't set class attribute

I read the 'How-To Guide for Descriptors'
(http://users.rcn.com/python/download/Descriptor.htm) that describes
the equivalent python implementation of property() and classmethod()
and I came up with this:

def classproperty(f unction):
class Descriptor(obje ct):
def __get__(self, obj, objtype):
return function(objtyp e)
def __set__(self, obj, value):
raise AttributeError, "can't set class attribute"
return Descriptor()

Accessing Foo.TheAnswer works as expected, however __set__ is
apparently not called because no exception is thrown when setting
Foo.TheAnswer. What am I missing here ?

George

Jul 21 '05 #1
3 2695
On 10 Jul 2005 13:38:22 -0700, "George Sakkis" <gs*****@rutger s.edu> wrote:
I'm trying to write a decorator similar to property, with the
difference that it applies to the defining class (and its subclasses)
instead of its instances. This would provide, among others, a way to
define the equivalent of class-level constants:

class Foo(object):
@classproperty
def TheAnswer(cls):
return "The Answer according to %s is 42" % cls.__name__
Foo.TheAnswerThe Answer according to Foo is 42 Foo.TheAnswer = 0

exceptions.Att ributeError
...
AttributeError : can't set class attribute

I read the 'How-To Guide for Descriptors'
(http://users.rcn.com/python/download/Descriptor.htm) that describes
the equivalent python implementation of property() and classmethod()
and I came up with this:

def classproperty(f unction):
class Descriptor(obje ct):
def __get__(self, obj, objtype):
return function(objtyp e)
def __set__(self, obj, value):
raise AttributeError, "can't set class attribute"
return Descriptor()

Accessing Foo.TheAnswer works as expected, however __set__ is
apparently not called because no exception is thrown when setting
Foo.TheAnswe r. What am I missing here ?

I suspect type(Foo).TheAn swer is not found and therefore TheAnswer.__set __ is not
looked for, and therefore it becomes an ordinary attribute setting. I suspect this
is implemented in object.__setatt r__ or type.__setattr_ _ as the case may be, when
they are inherited. So I introduced a __setattr__ in type(Foo) by giving Foo
a metaclass as its type(Foo). First I checked whether the attribute type name was
'Descriptor' (not very general ;-) and raised an attribute error if so.
Then I made a class Bar version of Foo and checked for __set__ and called that
as if a property of type(Bar) instances. See below.
----< classprop.py >----------------------------------------------------
def classproperty(f unction):
class Descriptor(obje ct):
def __get__(self, obj, objtype):
return function(objtyp e)
def __set__(self, obj, value):
raise AttributeError, "can't set class attribute"
return Descriptor()

class Foo(object):
class __metaclass__(t ype):
def __setattr__(cls , name, value):
if type(cls.__dict __.get(name))._ _name__ == 'Descriptor':
raise AttributeError, 'setting Foo.%s to %r is not allowed' %(name, value)
type.__setattr_ _(cls, name, value)
@classproperty
def TheAnswer(cls):
return "The Answer according to %s is 42" % cls.__name__
@classproperty
def AnotherAnswer(c ls):
return "Another Answer according to %s is 43" % cls.__name__

class Bar(object):
class __metaclass__(t ype):
def __setattr__(cls , name, value):
attr = cls.__dict__.ge t(name)
if hasattr(attr, '__set__'):
attr.__set__(cl s, value) # like an instance attr setting
else:
type.__setattr_ _(cls, name, value)
@classproperty
def TheAnswer(cls):
return "The Answer according to %s is 42" % cls.__name__
@classproperty
def AnotherAnswer(c ls):
return "Another Answer according to %s is 43" % cls.__name__
if __name__ == '__main__':
print Foo.TheAnswer
Foo.notTheAnswe r = 'ok'
print 'Foo.notTheAnsw er is %r' % Foo.notTheAnswe r
print Foo.AnotherAnsw er
try: Foo.TheAnswer = 123
except AttributeError, e: print '%s: %s' %(e.__class__._ _name__, e)
try: Foo.AnotherAnsw er = 456
except AttributeError, e: print '%s: %s' %(e.__class__._ _name__, e)
print Bar.TheAnswer
Bar.notTheAnswe r = 'ok'
print 'Bar.notTheAnsw er is %r' % Bar.notTheAnswe r
print Bar.AnotherAnsw er
try: Bar.TheAnswer = 123
except AttributeError, e: print '%s: %s' %(e.__class__._ _name__, e)
try: Bar.AnotherAnsw er = 456
except AttributeError, e: print '%s: %s' %(e.__class__._ _name__, e)
------------------------------------------------------------------------
Result:

[18:17] C:\pywk\clp>py2 4 classprop.py
The Answer according to Foo is 42
Foo.notTheAnswe r is 'ok'
Another Answer according to Foo is 43
AttributeError: setting Foo.TheAnswer to 123 is not allowed
AttributeError: setting Foo.AnotherAnsw er to 456 is not allowed
The Answer according to Bar is 42
Bar.notTheAnswe r is 'ok'
Another Answer according to Bar is 43
AttributeError: can't set class attribute
AttributeError: can't set class attribute

Regards,
Bengt Richter
Jul 21 '05 #2
Bengt Richter wrote:
....

class Foo(object):
class __metaclass__(t ype):
def __setattr__(cls , name, value):
if type(cls.__dict __.get(name))._ _name__ == 'Descriptor':
raise AttributeError, 'setting Foo.%s to %r is not allowed' %(name, value)
type.__setattr_ _(cls, name, value)
@classproperty
def TheAnswer(cls):
return "The Answer according to %s is 42" % cls.__name__
@classproperty
def AnotherAnswer(c ls):
return "Another Answer according to %s is 43" % cls.__name__


or, simply put the read-only descriptor in the metaclass:

Python 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright" , "credits" or "license" for more information.
def classproperty(f unction): ... class Descriptor(obje ct):
... def __get__(self, obj, objtype):
... return function(objtyp e)
... def __set__(self, obj, value):
... raise AttributeError, "can't set class attribute"
... return Descriptor()
... class A(object): ... class __metaclass__(t ype):
... @classproperty
... def TheAnswer(cls):
... return "The Answer according to %s is 42" % cls.__name__
... A.TheAnswer 'The Answer according to __metaclass__ is 42' A.TheAnswer = 3 Traceback (most recent call last):
File "<input>", line 1, in ?
File "<input>", line 6, in __set__
AttributeError: can't set class attribute class B(A): pass ... B.TheAnswer 'The Answer according to __metaclass__ is 42'

this means that the getter doesn't automatically get a reference to the class
(since it is a method of metaclass), which may or may not matter, depending on
the application

Michael

Jul 21 '05 #3
On Sun, 10 Jul 2005 21:10:36 -0700, Michael Spencer <ma**@telcopart ners.com> wrote:
Bengt Richter wrote:
...

class Foo(object):
class __metaclass__(t ype):
def __setattr__(cls , name, value):
if type(cls.__dict __.get(name))._ _name__ == 'Descriptor':
raise AttributeError, 'setting Foo.%s to %r is not allowed' %(name, value)
type.__setattr_ _(cls, name, value)
@classproperty
def TheAnswer(cls):
return "The Answer according to %s is 42" % cls.__name__
@classproperty
def AnotherAnswer(c ls):
return "Another Answer according to %s is 43" % cls.__name__


or, simply put the read-only descriptor in the metaclass:

Python 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright" , "credits" or "license" for more information.
>>> def classproperty(f unction): ... class Descriptor(obje ct):
... def __get__(self, obj, objtype):
... return function(objtyp e)
... def __set__(self, obj, value):
... raise AttributeError, "can't set class attribute"
... return Descriptor()
... >>> class A(object): ... class __metaclass__(t ype):
... @classproperty
... def TheAnswer(cls):
... return "The Answer according to %s is 42" % cls.__name__
... >>> A.TheAnswer 'The Answer according to __metaclass__ is 42' >>> A.TheAnswer = 3 Traceback (most recent call last):
File "<input>", line 1, in ?
File "<input>", line 6, in __set__
AttributeError: can't set class attribute >>> class B(A): pass ... >>> B.TheAnswer 'The Answer according to __metaclass__ is 42' >>>

this means that the getter doesn't automatically get a reference to the class
(since it is a method of metaclass), which may or may not matter, depending on
the application

It appears that you can use an ordinary property in the metaclass, and get the reference:
(I tried doing this but I still had the classproperty decorator and somehow inside a metaclass
it bombed or I typoed, and I forgot to try the plain property, so I hacked onwards to the
more involved __setattr__ override). Anyway,
class A(object): ... class __metaclass__(t ype):
... def TheAnswer(cls):
... return "The Answer according to %s is 42" % cls.__name__
... def __refuse(cls, v):
... raise AttributeError, "Refusing to set %s.TheAnswer to %r"%(cls.__name __, v)
... TheAnswer = property(TheAns wer, __refuse)
...
... A.TheAnswer 'The Answer according to A is 42' A.TheAnswer = 123 Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 6, in __refuse
AttributeError: Refusing to set A.TheAnswer to 123

Of course, access through an instance won't see this:
a=A()
a.TheAnswer Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: 'A' object has no attribute 'TheAnswer'

since TheAnswer is found in type(a)'s mro, but not type(A)'s:
type(a).mro() [<class '__main__.A'>, <type 'object'>]
type(A).mro() Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: descriptor 'mro' of 'type' object needs an argument

looks like you get type.mro as an unbound method that way...
type(A).mro(typ e(A)) [<class '__main__.__met aclass__'>, <type 'type'>, <type 'object'>]

or
type.mro(A) [<class '__main__.A'>, <type 'object'>] type.mro(type(A )) [<class '__main__.__met aclass__'>, <type 'type'>, <type 'object'>]

or even
type.__dict__['mro'] <method 'mro' of 'type' objects> type.__dict__['mro'](A) [<class '__main__.A'>, <type 'object'>] type.__dict__['mro'](type(A)) [<class '__main__.__met aclass__'>, <type 'type'>, <type 'object'>] type(A)

<class '__main__.__met aclass__'>

Regards,
Bengt Richter
Jul 21 '05 #4

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

Similar topics

2
9004
by: Gunnar | last post by:
Hello, I've just written a CPP program that reads integers from a binary file, and used this code while (my_ifstram.read( (char* ) &number, sizeof(int)) { // do something with number } My question is now, where can I find a manual that describes what the read method does with the ifstream object? I'm sitting here with my Linux/Debian machine, but I have not found any
6
3477
by: Steve | last post by:
Hi, I'm trying to convert a file reading loop into one using streams. The BSD OS read API returns the number of bytes read, but istream::read returns itself. How can I find out the number of bytes actually read? What the code fragment should do is read up to 1000 bytes into a buffer, or finish early if reading failed. Just your average read loop. I have: (this is a simplified version; I know there's no detailed error
12
11666
by: Steven T. Hatton | last post by:
I know of a least one person who believes std::ifstream::read() and std::ofstream::write() are "mistakes". They seem to do the job I want done. What's wrong with them. This is the code I currently have as a test for using std::ifstream::read(). Is there anything wrong with the way I'm getting the file? #include <vector> #include <iomanip> #include <fstream> #include <iostream>
2
3092
by: Sandman | last post by:
Just looking for suggestion on how to do this in my Web application. The goal is to keep track of what a user has and hasn't read and present him or her with new material I am currently doing this by aggregating new content from all databases into a single indexed database and then saving a timestamp in the account database (for the current user) that tells me when the user last read items in the aggregated database.
2
2509
by: Andrea Bauer | last post by:
Hallo, wie kann ich so eine Datei unter .Net schreiben C++ oder C#. Bitte mit Funktionsaufrufen. Vielen Dank. Grüße Andrea <Product> <ProgramNumber>2</ProgramNumber>
4
3847
by: Ollie Cook | last post by:
Hi, I am having some difficulty with read(2) and interrupting signals. I expect I am misunderstanding how the two work together, so would appreciate some guidance. I am trying to 'time out' a socket read after a certain delay. The logic is (I will provide a test program below): - create and connect socket
9
1977
by: ferbar | last post by:
Hi all, I'm trying to read from the txt file 'ip.packets.2.txt' using the read function. It seems everything ok, but I get a -1 when executing >>bytesr = read(fdo1, bufread, 2); The 'open' function returns the file dsc 3. So this seems ok.. Any idea what might be wrong?
0
4753
by: phplasma | last post by:
Hey, I am currently attempting to implement a multi-threaded C# socket, using SSL (.pem file/certification/private key combo) server using Visual Studio C# Express. I have successfully made the client application establish a connection, and send data, which appears in plain, de-crypted text on the server - this works.
4
2807
by: zl2k | last post by:
hi, there I have a appendable binary file of complex data structure named data.bin created by myself. It is written in the following format: number of Data, Data array Suppose I have following data.bin (3 Data appended to 2 Data): 2, data0, data1, 3, data0, data1, data2
5
12870
by: Thomas Christensen | last post by:
This issue has been raised a couple of times I am sure. But I have yet to find a satisfying answer. I am reading from a subprocess and this subprocess sometimes hang, in which case a call to read() call will block indefinite, keeping me from killing it. The folloing sample code illustrates the problem: proc = subprocess.Popen(,
0
10336
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...
1
10095
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,...
0
9953
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...
0
8978
agi2029
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...
0
6741
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();...
0
5383
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5513
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4054
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
3
2881
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.