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

Home Posts Topics Members FAQ

Attribute reference design

Hello.
I think I'm aware of how attribute access is resolved in python. When
referencing a class instance attribute which is not defined in the
scope of the instance, Python looks for a class attribute with the
same name. (For assignment or deletion this is not the case,
thankfully.)
I've been trying to understand why? What is the reason behind, or
practical purpose of, this design decision? Anyone, please enlighten
me.

/Henrik
Jul 1 '08 #1
8 1432
chamalulu schrieb:
Hello.
I think I'm aware of how attribute access is resolved in python. When
referencing a class instance attribute which is not defined in the
scope of the instance, Python looks for a class attribute with the
same name. (For assignment or deletion this is not the case,
thankfully.)
I've been trying to understand why? What is the reason behind, or
practical purpose of, this design decision? Anyone, please enlighten
me.
How else would you resolve methods which are of course defined on the
class but invoked through the instance?

Diez
Jul 1 '08 #2
On Jul 1, 11:24 pm, "Diez B. Roggisch" <de...@nospam.w eb.dewrote:
chamalulu schrieb:
Hello.
I think I'm aware of how attribute access is resolved in python. When
referencing a class instance attribute which is not defined in the
scope of the instance, Python looks for a class attribute with the
same name. (For assignment or deletion this is not the case,
thankfully.)
I've been trying to understand why? What is the reason behind, or
practical purpose of, this design decision? Anyone, please enlighten
me.

How else would you resolve methods which are of course defined on the
class but invoked through the instance?
Yes, of course... You're right.
Didn't think of that.
Thank you. I'll go stand in the corner. :)

I think I haven't got this bound/unbound stuff through my head yet. If
I dir() a class instance I see the methods right there. Are they not
bound to the class instance at instanciation (and as such be
attributes of the class instance)?

/Henrik
Jul 1 '08 #3
chamalulu wrote:
On Jul 1, 11:24 pm, "Diez B. Roggisch" <de...@nospam.w eb.dewrote:
>chamalulu schrieb:

>>Hello.
I think I'm aware of how attribute access is resolved in python. When
referencing a class instance attribute which is not defined in the
scope of the instance, Python looks for a class attribute with the
same name. (For assignment or deletion this is not the case,
thankfully. )
I've been trying to understand why? What is the reason behind, or
practical purpose of, this design decision? Anyone, please enlighten
me.
How else would you resolve methods which are of course defined on the
class but invoked through the instance?


Yes, of course... You're right.
Didn't think of that.
Thank you. I'll go stand in the corner. :)
No need. Also, you can define a class attribute (C++ might call it a
static attribute) and access it transparently through an instance.

class C:
aClassAttribute = 123
def __init__(self, ...):
...

c = C()
.... do something with c.aClassAttribu te ...
I think I haven't got this bound/unbound stuff through my head yet. If
I dir() a class instance I see the methods right there. Are they not
bound to the class instance at instanciation (and as such be
attributes of the class instance)?

/Henrik
--
http://mail.python.org/mailman/listinfo/python-list
Jul 1 '08 #4
On Jul 2, 1:17 am, Gary Herron <gher...@island training.comwro te:
No need. Also, you can define a class attribute (C++ might call it a
static attribute) and access it transparently through an instance.

class C:
aClassAttribute = 123
def __init__(self, ...):
...

c = C()
... do something with c.aClassAttribu te ...
Actually, this is why I started too look into the attribute reference
mechanics to begin with. Coming from mostly C# development I think
it's weird to be able to refer to class attributes (static members)
through a class instance (object). But I think I'm getting the
picture. Function objects lay flat in memory (some heap...). When
defined inside classes they are wrapped in method objects. When
refered through classes or class instances they are unbound method
objects or bound method objects respectively. Am I on the right track?
I still don't get why these methods show up when I dir() a class
instance.

/Henrik
Jul 1 '08 #5
chamalulu wrote:
On Jul 2, 1:17 am, Gary Herron <gher...@island training.comwro te:
>No need. Also, you can define a class attribute (C++ might call it a
static attribute) and access it transparently through an instance.

class C:
aClassAttribute = 123
def __init__(self, ...):
...

c = C()
... do something with c.aClassAttribu te ...


Actually, this is why I started too look into the attribute reference
mechanics to begin with. Coming from mostly C# development I think
it's weird to be able to refer to class attributes (static members)
through a class instance (object). But I think I'm getting the
picture. Function objects lay flat in memory (some heap...).
Not quite. Not *flat memory* or a heap. Modules are first class
objects, and functions (and classes and anything else) defined at the
top level of the module are *attributes* of the module.

Gary Herron

When
defined inside classes they are wrapped in method objects. When
refered through classes or class instances they are unbound method
objects or bound method objects respectively. Am I on the right track?
I still don't get why these methods show up when I dir() a class
instance.

/Henrik
--
http://mail.python.org/mailman/listinfo/python-list
Jul 1 '08 #6
chamalulu a écrit :
On Jul 2, 1:17 am, Gary Herron <gher...@island training.comwro te:
>No need. Also, you can define a class attribute (C++ might call it a
static attribute) and access it transparently through an instance.

class C:
aClassAttribute = 123
def __init__(self, ...):
...

c = C()
... do something with c.aClassAttribu te ...

Actually, this is why I started too look into the attribute reference
mechanics to begin with. Coming from mostly C# development I think
it's weird to be able to refer to class attributes (static members)
through a class instance (object). But I think I'm getting the
picture. Function objects lay flat in memory (some heap...).
Python's functions are ordinary objects, instance of type 'function'.
When
defined inside classes they are wrapped in method objects.
Nope. The wrapping happens at lookup time, thru the descriptor protocol
(the same thing that gives support for properties).
When
refered through classes or class instances they are unbound method
objects or bound method objects respectively.
That's what function.__get_ _() returns, yes. What is stored in the class
object's __dict__ is the plain function.
Am I on the right track?
I still don't get why these methods show up when I dir() a class
instance.
"""
Help on built-in function dir in module __builtin__:

dir(...)
dir([object]) -list of strings

Return an alphabetized list of names comprising (some of) the
attributes
of the given object, and of attributes reachable from it:

No argument: the names in the current scope.
Module object: the module attributes.
Type or class object: its attributes, and recursively the
attributes of
its bases.
Otherwise: its attributes, its class's attributes, and recursively the
attributes of its class's base classes.

"""
/Henrik
Jul 2 '08 #7
On Jul 2, 10:13 am, Bruno Desthuilliers <bruno.
42.desthuilli.. .@websiteburo.i nvalidwrote:
Nope. The wrapping happens at lookup time, thru the descriptor protocol
(the same thing that gives support for properties).
Aha, I should read up on that.
Help on built-in function dir in module __builtin__:
So, the dir function is a little more helpful than I thaught. I
checked instance.__dict __ now instead of dir(instance). No methods.
It's getting clearer. Thank you.

I think you've provided me with a good reason for class instances to
delegate attribute references to the class if class instance doesn't
contain the attribute. Diez, Gary, Bruno; Thanks for your help.

/Henrik
Jul 2 '08 #8
Le Wednesday 02 July 2008 01:17:21 Gary Herron, vous avez écrit*:
chamalulu wrote:
On Jul 1, 11:24 pm, "Diez B. Roggisch" <de...@nospam.w eb.dewrote:
chamalulu schrieb:
Hello.
I think I'm aware of how attribute access is resolved in python. When
referencing a class instance attribute which is not defined in the
scope of the instance, Python looks for a class attribute with the
same name. (For assignment or deletion this is not the case,
thankfully.)
I've been trying to understand why? What is the reason behind, or
practical purpose of, this design decision? Anyone, please enlighten
me.

How else would you resolve methods which are of course defined on the
class but invoked through the instance?
Yes, of course... You're right.
Didn't think of that.
Thank you. I'll go stand in the corner. :)

No need. Also, you can define a class attribute (C++ might call it a
static attribute) and access it transparently through an instance.

class C:
aClassAttribute = 123
def __init__(self, ...):
...

c = C()
... do something with c.aClassAttribu te ...
Be very careful with that, as it looks like C++ or similar other OO languages,
but python handles it in a strange way if you assign a value to such an
attribute. It took me a long time to understand what happens here:

class Foo (object) :
bar = 0

foo = Foo()
print '(1) Foo.bar: %d' % Foo.bar
print '(1) foo.bar: %d' % foo.bar

Foo.bar += 1
print '(2) Foo.bar: %d' % Foo.bar
print '(2) foo.bar: %d' % foo.bar

foo.bar += 1
print '(3) Foo.bar: %d' % Foo.bar
print '(3) foo.bar: %d' % foo.bar

here's the output:

(1) Foo.bar: 0
(1) foo.bar: 0
(2) Foo.bar: 1
(2) foo.bar: 1
(3) Foo.bar: 1 # hey dude, where is my bar ?
(3) foo.bar: 2

In the third case, you might expect foo.bar += 1 to just increment Foo.bar,
but actually it doesn't. I first thought it was a bug, but it's not. When you
write foo.bar += 1 (equivalent to foo.bar = foo.bar + 1), python first looks
for a bar attribute, finds it in the class members, and then creates an
instance member with the result. Things would be different with a mutable
type implementing the += operator.

I discovered this in a middle of a project and it was hard to track all these
assignments in my code to correct them, so I'd suggest to always access class
members through the class instead of the instance, unless you have no choice
or know exactly what you are doing.

--
Cédric Lucantis
Jul 2 '08 #9

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

Similar topics

7
1720
by: Brian Genisio | last post by:
Hello all, I am developing a class where speed is important, and memory size is also important. I need to give the user of the class read-only access to one of the private members (size as an int). I have two options: 1. Make a method : int getSize() {return m_size;} 2. Use a reference: const int &size;, and link it to the private size value Option #1 is nice, because it does not take an extra member, but it is
24
3531
by: Charles Crume | last post by:
Hello; My "index.htm" page has 3 frames (content, navigation bar, and logo). I set the "SRC" of the "logo" frame to a blank gif image and then want to change it's contents after the other two frames have been loaded by using a javascript statement from the "navigation" frame, as shown below: top.window.ccs_logo.src = 'images/ccs_logo.gif'; alert(top.window.ccs_logo.src);
0
1066
by: Jeff User | last post by:
Here is what I have and what I would like to do: I am building a web client application. I add a web reference to some web service server to my project so that now I can call the web service. A proxy class is generated for me. This is nice. It works! Now, the method that is generated in the proxy class has an attribute preceding it that, among other things, contains the web address of the web service and the particular method at that...
1
6539
by: D Ratcliffe | last post by:
Hello In a nutshell, I am looping through an XML document in a for loop, and I want to insert a given value as an element to each tag as I go along. i.e.: for (int i=0; i < (rtnX.GetElementsByTagName("list").Count); i++) { // I want here to add the attribute "display" to the selected tag }
2
2135
by: Jeff User | last post by:
Here is what I have and what I would like to do: I am building a web client application. I add a web reference to some web service server to my project so that now I can call the web service. A proxy class is generated for me. This is nice. It works! Now, the method that is generated in the proxy class has an attribute preceding it that, among other things, contains the web address of the web service and the particular method at that...
6
2153
by: ken.carlino | last post by:
>From http://www.parashift.com/c++-faq-lite/references.html#faq-8.6, it said "Use references when you can, and pointers when you have to." And I need to convert this java class to c++: public class Y { public final int w public Y(final List alist { w = new int;
2
9620
by: keith | last post by:
In an MSDN article on high quality design principles combined with standards driven code, Microsoft states: that the image title attribute should display when users roll over the image and that Non-visual browsers will read this text to people who are blind. In my attempts to implement this I have found that the ASP.Img control does not have an attribute for title. It is necessary to use the ASP:IMG control instead of the html img tag in...
3
2031
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...
3
1999
by: kjell | last post by:
Hi, I wonder if someone may have a solution to a small problem I'm experiencing with a C++ program? I'm have two classes in my application that provides methods for setting parameters in the classes. These methods returns a pointer to the class itself so that you can use the return value to call another attribute setting method on the same line. This is what my program look like: #include <iostream>
0
9643
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, 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...
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10319
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...
0
10147
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 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...
1
10087
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
6737
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
5380
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...
1
4046
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
2877
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.