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

Re: ClassName.attribute vs self.__class__.attribute


"Casey McGinty" <ca***********@gmail.comwrote in message
news:ae******************************************@ mail.gmail.com...
| On Thu, Jun 5, 2008 at 5:40 AM, Gabriel Rossetti <
| ga**************@arimaz.comwrote:
|
| Hello everyone,
| >
| I had read somewhere that it is preferred to use
self.__class__.attribute
| over ClassName.attribute to access class (aka static) attributes. I had
done
| this and it seamed to work, until I subclassed a class using this
technique
| and from there on things started screwing up. I finally tracked it down
to
| self.__class__.attribute! What was happening is that the child classes
each
| over-rode the class attribute at their level, and the parent's was
never
| set, so while I was thinking that I had indeed a class attribute set in
the
| parent, it was the child's that was set, and every child had it's own
| instance! Since it was a locking mechanism, lots of fun to debug... So,
I
| suggest never using self.__class__.attribute, unless you don't mind
it's
| children overriding it, but if you want a truly top-level class
attribute,
| use ClassName.attribute everywhere!
|
| Thanks for the info. Can anyone explain more about the differences
between
| the two techniques? Why does one work and the other one fail?

If you want to access the attribute of a particular class, to read or
write, use that class.
SomeClass.attr
Note that no instance is required or relevant.

If you want to read the attrubute of the class of an instance (or the first
superclass with the attribute, whatever that class might be, use self.attr
or self.__class__.attr. (Use the latter if the instance has (or might
have) an attribute of the same name).

For setting an attribute, self.attr = x sets it on the instance while
self.__class__.attr = x sets it on its class.

Jun 27 '08 #1
0 1044

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

Similar topics

2
by: Gabriel Genellina | last post by:
Hi In the following code sample, I have: - a Worker class, which could have a lot of methods and attributes. In particular, it has a 'bar' attribute. This class can be modified as needed. - a...
7
by: svilen | last post by:
hello again. i'm now into using python instead of another language(s) for describing structures of data, including names, structure, type-checks, conversions, value-validations, metadata etc....
13
by: Frans Englich | last post by:
Hello, I am having trouble with throwing class instances around. Perhaps I'm approaching my goals with the wrong solution, but here's nevertheless a stripped down example which demonstrates my...
3
by: MackS | last post by:
I'm new to Python. In general I manage to understand what is happening when things go wrong. However, the small program I am writing now fails with the following message: AttributeError: ClassA...
11
by: Christopher J. Bottaro | last post by:
I actually want all the parent classes too. So if D derives off C derives off B derives off A, I ultimately want a tuple ('D', 'C', 'B', 'A'). For those of you following the Python Documentation...
4
by: Ben R. | last post by:
Between ClassName and Inherits, which attribute is set to specify the class that a page uses? I would think that would be inherits. Further, the description for ClassName is: Specifies the class...
6
by: Adam Donahue | last post by:
As an exercise I'm attempting to write a metaclass that causes an exception to be thrown whenever a user tries to access 'attributes' (in the traditional sense) via a direct reference. Consider:...
18
by: Gabriel Rossetti | last post by:
Hello everyone, I had read somewhere that it is preferred to use self.__class__.attribute over ClassName.attribute to access class (aka static) attributes. I had done this and it seamed to work,...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.