Hello,
I'm probably missing something that should be obvious, but can
anyone tell me what's wrong with the following?
temp.py
---------------------------------------------------------------------
class Item(object):
def __init__(self, id, data):
self.id = id
self.data = data
class KeyedSet(dict):
def __init__(self, items=None):
if items is not None:
for item in items:
self[item.id] = item
def __iter__(self):
return self.itervalues()
def __repr__(self):
return '%s(%r)' % (self.__class__.__name__, self.keys())
def __contains__(self, item):
return self.has_key(item.id)
def intersection(self, other):
res = self.__class__()
for item in self:
if item in other:
res[item.id] = item
return res
def __and__(self, other):
return self.intersection(other)
def intersection_update(self, other):
self &= other
def __iand__(self, other):
self = self.intersection(other)
return self
-------------------------------------------------------------------- from temp import Item, KeyedSet a = Item(0, 'W') b = Item(1, 'X') c = Item(2, 'Y') d = Item(3, 'Z') aset = KeyedSet([a, b, c]) bset = KeyedSet([b, c, d]) aset &= bset aset
KeyedSet([1, 2]) aset = KeyedSet([a, b, c]) aset.intersection_update(bset) aset
KeyedSet([0, 1, 2])
I can't figure out why 'aset' is unchanged? Thanks.
Duncan 3 1254
Duncan Smith wrote: I'm probably missing something that should be obvious, but can anyone tell me what's wrong with the following?
temp.py ---------------------------------------------------------------------
class Item(object): def __init__(self, id, data): self.id = id self.data = data
class KeyedSet(dict): def __init__(self, items=None): if items is not None: for item in items: self[item.id] = item
def __iter__(self): return self.itervalues()
def __repr__(self): return '%s(%r)' % (self.__class__.__name__, self.keys())
def __contains__(self, item): return self.has_key(item.id)
def intersection(self, other): res = self.__class__() for item in self: if item in other: res[item.id] = item return res
def __and__(self, other): return self.intersection(other)
def intersection_update(self, other): self &= other
def __iand__(self, other): self = self.intersection(other) return self --------------------------------------------------------------------
from temp import Item, KeyedSet a = Item(0, 'W') b = Item(1, 'X') c = Item(2, 'Y') d = Item(3, 'Z') aset = KeyedSet([a, b, c]) bset = KeyedSet([b, c, d]) aset &= bset aset KeyedSet([1, 2]) aset = KeyedSet([a, b, c]) aset.intersection_update(bset) aset KeyedSet([0, 1, 2]) I can't figure out why 'aset' is unchanged? Thanks.
Your problem stripped down to the bare bones: __iand__() creates a new
instance instead of modifying the current one.
self = something
doesn't copy something's data to self, it just rebinds self to something for
the rest of the method.
A minimal example of what went wrong: class A:
.... def __init__(self, value):
.... self.value = value
.... def __iand__(self, other):
.... return A(self.value + other.value)
.... def __repr__(self):
.... return "value=%s" % self.value
.... a = b = A(1) a &= A(2)
It looks like inplace modification, but isn't:
a, b
(value=3, value=1)
a is rebound to the newly created instance, which tricks you into believing
it was modified. The backup reference b reveals the error.
Here's the correction (I'm lazy, so I reuse the unaltered parts of A):
class B(A):
.... def __iand__(self, other):
.... self.value += other.value
.... return self
.... a = b = B(1) a &= B(2) a, b
(value=3, value=3)
Once you understand the basic principle, it should be no problem to apply it
to your KeyedSet class. If in doubt, use the sets.Set implementation as a
template.
Peter
"Peter Otten" <__*******@web.de> wrote in message
news:c9*************@news.t-online.com... Duncan Smith wrote:
I'm probably missing something that should be obvious, but can anyone tell me what's wrong with the following?
temp.py ---------------------------------------------------------------------
class Item(object): def __init__(self, id, data): self.id = id self.data = data
class KeyedSet(dict): def __init__(self, items=None): if items is not None: for item in items: self[item.id] = item
def __iter__(self): return self.itervalues()
def __repr__(self): return '%s(%r)' % (self.__class__.__name__, self.keys())
def __contains__(self, item): return self.has_key(item.id)
def intersection(self, other): res = self.__class__() for item in self: if item in other: res[item.id] = item return res
def __and__(self, other): return self.intersection(other)
def intersection_update(self, other): self &= other
def __iand__(self, other): self = self.intersection(other) return self --------------------------------------------------------------------
> from temp import Item, KeyedSet > a = Item(0, 'W') > b = Item(1, 'X') > c = Item(2, 'Y') > d = Item(3, 'Z') > aset = KeyedSet([a, b, c]) > bset = KeyedSet([b, c, d]) > aset &= bset > aset KeyedSet([1, 2])> aset = KeyedSet([a, b, c]) > aset.intersection_update(bset) > aset KeyedSet([0, 1, 2])>
I can't figure out why 'aset' is unchanged? Thanks.
Your problem stripped down to the bare bones: __iand__() creates a new instance instead of modifying the current one.
self = something
doesn't copy something's data to self, it just rebinds self to something
for the rest of the method.
A minimal example of what went wrong:
class A: ... def __init__(self, value): ... self.value = value ... def __iand__(self, other): ... return A(self.value + other.value) ... def __repr__(self): ... return "value=%s" % self.value ... a = b = A(1) a &= A(2) It looks like inplace modification, but isn't: a, b (value=3, value=1)
a is rebound to the newly created instance, which tricks you into
believing it was modified. The backup reference b reveals the error. Here's the correction (I'm lazy, so I reuse the unaltered parts of A): class B(A): ... def __iand__(self, other): ... self.value += other.value ... return self ... a = b = B(1) a &= B(2) a, b (value=3, value=3)
Once you understand the basic principle, it should be no problem to apply
it to your KeyedSet class. If in doubt, use the sets.Set implementation as a template.
Peter
Yep. This also explains why the other unit tests involving __iand__ passed,
leaving me thinking it was OK. Cheers.
Duncan
"Duncan Smith" <bu*****@urubu.freeserve.co.uk> wrote in message news:<c9**********@news8.svr.pol.co.uk>... Hello, I'm probably missing something that should be obvious, but can anyone tell me what's wrong with the following?
temp.py ---------------------------------------------------------------------
class Item(object): def __init__(self, id, data): self.id = id self.data = data
class KeyedSet(dict): def __init__(self, items=None): if items is not None: for item in items: self[item.id] = item def __iter__(self): return self.itervalues() def __repr__(self): return '%s(%r)' % (self.__class__.__name__, self.keys()) def __contains__(self, item): return self.has_key(item.id) def intersection(self, other): res = self.__class__() for item in self: if item in other: res[item.id] = item return res def __and__(self, other): return self.intersection(other) def intersection_update(self, other): self &= other def __iand__(self, other): self = self.intersection(other) return self --------------------------------------------------------------------
from temp import Item, KeyedSet a = Item(0, 'W') b = Item(1, 'X') c = Item(2, 'Y') d = Item(3, 'Z') aset = KeyedSet([a, b, c]) bset = KeyedSet([b, c, d]) aset &= bset aset KeyedSet([1, 2]) aset = KeyedSet([a, b, c]) aset.intersection_update(bset) aset KeyedSet([0, 1, 2])
I can't figure out why 'aset' is unchanged? Thanks.
Your __iand__ method doesn't work the way that you think it does.
Remember, "self" is just another local variable name, so your function
works as if you had done:
def __iand__(self, other):
x = self.intersection(other)
return x
which (incorrectly) has the same effect as __and__. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Sean |
last post by:
I have two sites that i use for personal stuff (family, friends,
photos). They are PHP sites butim not a programmer. They were setup
by a friend who no longer helps with them. There are some...
|
by: Gary Herron |
last post by:
Hi list,
Can someone who has built the SpreadModule on a windows machine please
send me the results of the build (or just point me an a binary
distribution).
Here's why:
I'm starting to...
|
by: mike |
last post by:
regards:
excuse me,can someone introduce me some xhtml webs?
i want to see the xhtml web http response.
i know one xhtml web "http://www.w3c.org"
could someone introduce me some xhtml...
|
by: Janne Naukkarinen |
last post by:
Have someone commercial Digital
Mars (DMC) IDDE?
I need help making makefiles, it is
easier with IDDE.
However, IDDE is not freely distributed on net.
There is current WinVN WIP:
|
by: TCMA |
last post by:
I am looking for some tools to help me understand source code of a
program written in C++ by someone else.
Are there any non-commercial, open source C or C++ tools to reverse
engineer C or C++...
|
by: Raziq Shekha |
last post by:
Hello all,
Is there a way to figure out when was the last time someone connected to
a database? SQL 2000 environment.
Thanks,
Raziq.
|
by: MarcJessome |
last post by:
Hi, I was wondering if someone could help me through learning C++.
I've tried learning before, but I find I would work better if I had
someone that could help explain a few things to me. Im using...
|
by: Miles Keaton |
last post by:
I'm switching to PostgreSQL from MySQL. Using the SAMs book called
PostgreSQL which has been great to skim the surface of the
differerences.
I had never even heard of things like triggers,...
|
by: Robert Blackwell |
last post by:
Hey, I have a question for you web devs out there. How much of a hassle is
it to take over where another programmer left off.
I have a commerce site that was designed to my specs and I approached...
|
by: Apolakkiatis |
last post by:
I was experimenting around and tried to make it so that if someone presses the F key on their keyboard it also sends the rest of the letters to complete F*** anytime someone presses that letter... I...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
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: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
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:
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...
|
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...
| |