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

__getattribute__ for class object

hello
when i define __getattribute__ in a class, it is for the class instances
but if i want to have a __getattribute__ for class attributes

how can i do that ?

sylvain
Aug 12 '05 #1
3 2561
Sylvain Ferriol wrote:
hello
when i define __getattribute__ in a class, it is for the class instances
but if i want to have a __getattribute__ for class attributes

how can i do that ?


Skating on thin ice eh.Read something on metaclasses.
class Meta(type):
def __getattribute__(klass,attr):
value=type.__getattribute__(klass,attr)
print attr,'==',value
return value

class Foo(object):
__metaclass__=Meta
a=2

Foo.a

Paolino
Aug 12 '05 #2
Dan
> > but if i want to have a __getattribute__ for class attributes

Read something on metaclasses.


Depending on what you want to do, it might be better to use properties
instead:

class Meta(type):
x = property(lambda klass: 'Called for '+str(klass))

class Foo(object):
__metaclass__=Meta

print Foo.x

--
Do I know what's in this bill? Are you kidding? Only God knows...
- U.S. Senator Robert Byrd, when asked if he knew the
contents of a $520 billion, 4000-page spending bill
Aug 12 '05 #3
Dan wrote:
Depending on what you want to do, it might be better to use properties
instead:

class Meta(type):
x = property(lambda klass: 'Called for '+str(klass))

class Foo(object):
__metaclass__=Meta


Also worth noting that you can inline the metaclass if you don't need it
anywhere else, e.g.:

class Foo(object):
class __metaclass__(type):
x = property(lambda klass: 'Called for '+str(klass))

STeVe
Aug 12 '05 #4

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

Similar topics

24
by: Hung Jung Lu | last post by:
Hi, Does anybody know where this term comes from? "First-class object" means "something passable as an argument in a function call", but I fail to see the connection with "object class" or...
2
by: Krzysztof Stachlewski | last post by:
I tried to run the following piece of code: Python 2.3.4 (#53, May 25 2004, 21:17:02) on win32 Type "help", "copyright", "credits" or "license" for more information. >>> o = object() >>> o.a...
106
by: A | last post by:
Hi, I have always been taught to use an inialization list for initialising data members of a class. I realize that initialsizing primitives and pointers use an inialization list is exactly the...
1
by: Zhongqi Pan | last post by:
I am facing a problem- I am using f1(this) to pass a class object pointer to a function ( say f1) f1 is defined as follows
9
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I...
1
by: ypjofficial | last post by:
Dear All, According to OOPs , a base class pointer can to point to derived class object....call this as fact1 But somehow I am not comfortable while understanding this concept. The explanaition...
6
by: Brad | last post by:
I am creating a class (not a control) which implements IStateManager. I've created the class and all of the implementations (LoadView, SaveViewState, etc...). My Question is: How do I...
2
by: Steve | last post by:
Hello, If I instantiate a class object from a form I would like to be able to write to a label on the calling form something like "hello from class object" from within a subroutine inside...
4
by: Jeff | last post by:
The derived class below passes a reference to an object in its own class to its base calss constructor. The code compiles and will run successfully as long as the base class constructor does not...
13
by: Rahul | last post by:
Hi Everyone, I was just playing around virtual functions and landed up with the following, class Base1 { public: virtual void sample() { printf("base::sample\n");
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
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...
0
jinu1996
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...
0
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...
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: 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...

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.