With this little snippet, i get an inconsistency between the behavior
of string and dictionary class variables:
Python 2.2.3 (#42, May 30 2003, 18:12:08) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
IDLE 0.8 -- press F1 for help class foo:
dict = {}
string = "foostring"
def bar(self):
self.dict["bar-key"] = "bar-value"
self.string = "bar-string"
# Lets create an instance of Foo baz = foo() baz.dict
{} baz.string
'foostring' # No suprises yet, the class variables are shown
# Now lets call bar() and change some vars baz.bar() baz.dict
{'bar-key': 'bar-value'} # Did it return a class variable or an instance variable? baz.__class__.dict
{'bar-key': 'bar-value'} # As you can see, both show the same values
baz.dict is baz.__class__.dict
1 # So we see that both are actually the same instance # of the dictionary class
# Now we look at the string baz.string
'bar-string' # Obviously this was the instance variable baz.__class__.string
'foostring' # And this was the class variable baz.string is baz.__class__.string
0 # And we actually can see, that they are different # instances of the string class
Question: Which behavior is the correct one?
Question: Is this a bug or a feature? 4 1846
Eric Baker wrote: With this little snippet, i get an inconsistency between the behavior of string and dictionary class variables:
Python 2.2.3 (#42, May 30 2003, 18:12:08) [MSC 32 bit (Intel)] on win32 Type "copyright", "credits" or "license" for more information. IDLE 0.8 -- press F1 for help class foo: dict = {} string = "foostring" def bar(self):
Here you are changing the value of foo.dict. The equivalent to the following
line where you are rebinding the value of self.string would be:
self.dict = {"bar-key": "bar-value}
self.dict["bar-key"] = "bar-value" self.string = "bar-string"
But there's a twist: when you say fooinstance.attribute, attribute ist first
looked up in the instance, and then if not found, in the class. So
depending on the context, fooinstance.attribute may refer to a class or an
instance attribute. On the other hand, fooinstance.attribute = somevalue
will always rebind the instance attribute, to rebind the class attribute
you can do
foo.attribute = somevalue
or
fooinstance.__class__.attribute = somevalue
Question: Which behavior is the correct one?
Both.
Question: Is this a bug or a feature?
Feature that bugs you?
Peter
# on python 2.3.2
class foo (object) :
dict = {}
string = "foostring"
def bar(self):
self.dict["bar-key"] = "bar-value"
# is the same as
# self.dict.__setitem__( "bar-key" , "bar-value" )
# which tries to look up self.dict
# try:
# self.dict2["bar-key"] = "bar-value"
# which results in an attribute error
self.string = "bar-string"
# is adding an attribute string to the instance
# foo.string remains accessible
baz = foo()
baz.string is foo.string => True
baz.bar()
baz.string is foo.string => False
bye,
rm
Thank you Peter and Roel,
I don't believe this stumped me for hours. I guess that happens if you stay
up too late.
Basically what it boils down to, is that the operater "=" is doing different
things.
With the expression:
self.dict["bar-key"] = "bar-value"
You are modifying an existing instance of dict.
Wheras with this experession:
self.string = "bar-string"
You are actually creating a new instance, because strings are immutable the
"=" operater does not modify the string but actually creates a new one
within the current scope.
self.string = "bar-string"
is actually
self.string = str("bar-string")
and
self.dict["bar-key"] = "bar-value"
is actually
self.dict.__setitem__("bar-key" , "bar-value" )
Thanks for your help.
On Sun, 16 Nov 2003 11:29:19 -0500, Eric Baker wrote: Basically what it boils down to, is that the operater "=" is doing different things.
No, the '=' operator is being consistent -- it always assigns a new
value to whatever is on the lhs (left-hand side).
What is inconsistent is the lhs object you are assigning to.
With the expression: self.dict["bar-key"] = "bar-value"
Because you're not putting the dict itself on the lhs; you're referring
to one of its indexed values.
If, instead, you put:
self.dict = { "foo": "bar-value" }
this *would* be an equivalent operation to:
self.string = "bar-value"
Wheras with this experession: self.string = "bar-string" You are actually creating a new instance, because strings are immutable the "=" operater does not modify the string but actually creates a new one within the current scope.
As would happen if you did the same thing to a dict.
--
\ "If you get invited to your first orgy, don't just show up |
`\ nude. That's a common mistake. You have to let nudity |
_o__) 'happen.'" -- Jack Handey |
Ben Finney <http://bignose.squidly.org/> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Corey Lubin |
last post by:
someGlobal=1
class Original:
def foo(self):
# Make use of someGlobal
from original import *
someGlobal=2
|
by: Ali Eghtebas |
last post by:
Hi,
I have 3 questions regarding the code below:
1) Why can't I trap the KEYDOWN while I can trap KEYUP?
2) Is it correct that I use Return True within the IF-Statement? (I've
already read...
|
by: ogs |
last post by:
Why it's not possible to override operator "=" in C#? I have Class - Class1
with one property Prop1. For example
Class1 x = new Class1(); x.Prop1 = "this is x";
Class1 y = new Class1(); y.Prop1...
|
by: Nimmi Srivastav |
last post by:
There's a rather nondescript book called "Using Borland C++" by Lee
and Mark Atkinson (Que Corporation) which presents an excellent
discussion of overloaded new and delete operators. In fact there...
|
by: muscha |
last post by:
I don't get it. What's the main differences between overriding a base
class's methods vs. hiding them?
Thanks,
/m
|
by: Kamen Yotov |
last post by:
hi all,
i first posted this on
http://msdn.microsoft.com/vcsharp/team/language/ask/default.aspx
(ask a c# language designer) a couple of days ago, but no response so far...
therefore, i am...
|
by: Amin Sobati |
last post by:
Hi,
I have two classes. Class2 inhertis Class1:
-----------------------------
Public Class Class1
Public Overridable Sub MySub()
End Sub
End Class
Public Class Class2
|
by: r035198x |
last post by:
The Object class has five non final methods namely equals, hashCode, toString, clone, and finalize.
These were designed to be overridden according to specific general contracts. Other classes that...
|
by: xamalek |
last post by:
I was thinking that when you override a struct variable when you are
subclassing the struct, then the variable in the new struct would
stomp on the old one ... (i.e.) the parent variable (i) and...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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,...
| |