473,775 Members | 2,570 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

setting an attribute

"When you bind (on either a class or an instance) an attribute whose
name is not special...you affect only the __dict__ entry for the
attribute(in the class or instance, respectively)."

In light of that statement, how would one explain the output of this
code:

class Test(object):
x = [1, 2]

def __init__(self):
self.x[0] = 10

print Test.__dict__ #{.....'x':[1,2]....}
t = Test()
print t.x #[10, 2]
print t.__dict__ #{}
print Test.__dict__ #{.....'x':[10,2]...}

It looks to me like self.x[0] is binding on an instance whose
attribute name is not special, yet it doesn't affect any __dict__
entry for the attribute in the instance--instead it is affecting a
__dict__ entry for the attribute in the class.

May 16 '07 #1
3 1576
7stud a écrit :
"When you bind (on either a class or an instance) an attribute whose
name is not special...you affect only the __dict__ entry for the
attribute(in the class or instance, respectively)."

In light of that statement, how would one explain the output of this
code:

class Test(object):
x = [1, 2]

def __init__(self):
self.x[0] = 10

print Test.__dict__ #{.....'x':[1,2]....}
t = Test()
print t.x #[10, 2]
print t.__dict__ #{}
print Test.__dict__ #{.....'x':[10,2]...}

It looks to me like self.x[0] is binding on an instance whose
attribute name is not special,

self.x[0] = 10 doesn't bind self.x - it's just syntactic sugar for
self.x.__setite m__(0, 10) (which itself is syntactic sugar for
list.__setitem_ _(self.x, 0, 10))
yet it doesn't affect any __dict__
entry for the attribute in the instance
Of course. The name 'x' is looked up in the instance, then in the class.
Since there's no binding (only a method call on a class attribute),
instance's dict is not affected.

May 16 '07 #2
On May 16, 12:34 am, 7stud <bbxx789_0...@y ahoo.comwrote:
"When you bind (on either a class or an instance) an attribute whose
name is not special...you affect only the __dict__ entry for the
attribute(in the class or instance, respectively)."

In light of that statement, how would one explain the output of this
code:

class Test(object):
x = [1, 2]

def __init__(self):
self.x[0] = 10

print Test.__dict__ #{.....'x':[1,2]....}
t = Test()
print t.x #[10, 2]
print t.__dict__ #{}
print Test.__dict__ #{.....'x':[10,2]...}

It looks to me like self.x[0] is binding on an instance whose
attribute name is not special, yet it doesn't affect any __dict__
entry for the attribute in the instance--instead it is affecting a
__dict__ entry for the attribute in the class.
I think it's following scope rules. It can't find an attribute for
self.x in the instance. It then checks the class for the var and
finds it and sets it there. It would error otherwise...
>>class Test(object):
.... def __init__(self):
.... self.x[0] =7
....
>>t = Test()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in __init__
AttributeError: 'Test' object has no attribute 'x'

~Sean

May 16 '07 #3
On May 16, 2:24 am, Bruno Desthuilliers <bruno.
42.desthuilli.. .@wtf.websitebu ro.oops.comwrot e:
7stud a écrit :
"When you bind (on either a class or an instance) an attribute whose
name is not special...you affect only the __dict__ entry for the
attribute(in the class or instance, respectively)."
In light of that statement, how would one explain the output of this
code:
class Test(object):
x = [1, 2]
def __init__(self):
self.x[0] = 10
print Test.__dict__ #{.....'x':[1,2]....}
t = Test()
print t.x #[10, 2]
print t.__dict__ #{}
print Test.__dict__ #{.....'x':[10,2]...}
It looks to me like self.x[0] is binding on an instance whose
attribute name is not special,

self.x[0] = 10 doesn't bind self.x - it's just syntactic sugar for
self.x.__setite m__(0, 10) (which itself is syntactic sugar for
list.__setitem_ _(self.x, 0, 10))
yet it doesn't affect any __dict__
entry for the attribute in the instance

Of course. The name 'x' is looked up in the instance, then in the class.
Since there's no binding (only a method call on a class attribute),
instance's dict is not affected.
Thanks.

May 16 '07 #4

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

Similar topics

4
6012
by: G. Richard Bellamy | last post by:
I'm trying to unset the Encrypted attribute on all the files in a path. The attribute is not getting set. What am I doing wrong? Perhaps there's another newsgroup I can send this to? Here's the code: ============================================= using System;
3
1475
by: AndyG | last post by:
I'm trying to mess about with a table using javascript. I'm deleting the current row then adding a new row and adding a couple of columns. I then want to add either a style attribute and add a border to the bottom of one of the cells or add a class attribute and set it up in the stylesheet. Problem is, when I try to use the setAttribute it doesn't do anything. In fact when I try and set any attribute of cellLeft or cellRight such as...
0
1061
by: Edward Diener | last post by:
Since a __property can be split between a read access and a write access property in MC++, how does one set an attribute on a __property ? Is setting an attribute before either the read access function or the write access function sufficient to setting that attribute for the __property as a whole, or is there another syntax that works instead ?
3
1951
by: Patrick | last post by:
I am dynamically creating TextArea and drop-down lists in ASP.NET using something like HtmlTextArea eachTextArea = new HtmlTextArea(); I tried to set the "name" of these TextAreas, etc. (e.g. <textarea name="NameOfTextArea" rows="5" cols="60">some text area</textarea>), as I want to then dynamically read back the controls' values Setting the ID attributes doesn't work, nor does the following:...
0
1182
by: Torsten Sturm | last post by:
Hello, SP1 of Windows Server introduced a new "feature" in the GlobalizationConfig class called EnableBestFitResponseEncoding. Effect is that when you set anything other than UTF8 in web.config, e.g. <globalization requestEncoding="iso-8859-1" responseEncoding="iso-8859-1" />, many functions will break (including HttpUtility.URLDecode) with a MethodNotImplementedExecption in the new internal class
7
4284
by: Jazz | last post by:
Hello, I am using VSNet 2003 and I am trying to programmatically detect if debug attribute in the compilation tag is true or false. How can I do this ? Thanks, Jazz
3
1482
by: Valvalis | last post by:
Hello, I am trying to set a class attribute of a text.item element to the value of its nearest ancestor. I want to do this in the case that the class of the text.item is currently a blank string. <text class="read-only"> ... <text.item class=""/> //this element should inherit the class attribute "read-only" from text <foo class="read-write"> <text.item class="" /> //this element should inherit the class attribute...
5
1707
by: tleeuwenburg | last post by:
I have an object and wish to set an attribute on it which, unfortunately for me, is read-only. How can I go about this? Cheers. -T
4
1235
by: mk | last post by:
Hello everyone, I try to set two properties, "value" and "square" in the following code, and arrange it in such way that setting one property also sets another one and vice versa. But the code seems to get Python into infinite loop: def __init__(self, val): self._internalval=val self.square=pow(self._internalval,2)
0
9622
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
10268
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
10048
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
8939
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...
1
7464
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5360
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
5486
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3611
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2853
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.