473,795 Members | 3,386 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

mutable numeric type

There has been quite some traffic about mutable and immutable data types
on this list. I understand the issues related to mutable numeric data
types. However, in my special case I don't see a better solution to the
problem.
Here is what I am doing:

I am using a third party library that is performing basic numerical
operations (adding, multiplying, etc.) with objects of unknown type. Of
course, the objects must support the numerical operators. In my case the
third party library is a graph algorithm library and the assigned
objects are edge weights. I am using the library to compute node
distances, etc.

I would like to be able to change the edge weights after creating the
edges. Otherwise, I would have to remove the edges and re-create them
with the new values, which is quite costly. Since I also didn't want to
change the code of the graph library, I came up with a mutable numeric
type, which implements all the numerical operators (instances are of
course not hashable). This allows me to change the edge weights after
creating the graph.

I can do the following:
>>x = MutableNumeric( 10)
y = MutableNumeric( 2)
x*y
20
>>x.value = 1.3
x*y
2.6000000000000 001
>>>
The effect of numerical operations is determined by the contained basic
data types:
>>x.value = 3
x/2
1
>>x.value = 3.0
x/2
1.5
>>>
Augmented operations change the instance itself:
>>x.value = 0
id(x)
-1213448500
>>x += 2
x
MutableNumeric( 2)
>>id(x) # show that same instance
-1213448500
>>>
Is there anything wrong with such design? I am a bit surprised that
Python does not already come with such data type (which is really simple
to implement). Is there something that I am missing here?

Thanks!
Andreas

Jan 2 '07 #1
5 1620
On Mon, 01 Jan 2007 19:20:21 -0800, Andreas Beyer wrote:
I am using a third party library that is performing basic numerical
operations (adding, multiplying, etc.) with objects of unknown type. Of
course, the objects must support the numerical operators. In my case the
third party library is a graph algorithm library and the assigned
objects are edge weights. I am using the library to compute node
distances, etc.

I would like to be able to change the edge weights after creating the
edges. Otherwise, I would have to remove the edges and re-create them
with the new values, which is quite costly.
You've measured it or you're guessing?

Presumably the edges and/or nodes store the weights somewhere. Why not
just reassign the weight directly in place?

It isn't easy to judge whether your scheme is good bad or indifferent when
we know so little about the graph library you are using.

Since I also didn't want to change the code of the graph library,
You could subclass the graph class.

Another possibility is to dynamically modify the library, without changing
its source code. E.g.
from GraphLibrary import GraphWalker as _gw
import GraphLibrary

def myGraphWalker(a rgs):
x = _gw(args)
do_something_to (x)
return x

GraphLibrary.Gr aphWalker = myGraphWalker
# now use GraphWalker as normal, except it has your
# code instead of the original

This works for class methods as well.

I came up with a mutable numeric
type, which implements all the numerical operators (instances are of
course not hashable). This allows me to change the edge weights after
creating the graph.
This is another alternative, although I still don't understand why you
can't just reassign the weights in place.
--
Steven D'Aprano

Jan 2 '07 #2
Way to go.
Try doing this.
x = MutableNumeric( 42)
y = x
x += 42
print y

Jan 2 '07 #3
pg******@acay.c om.au wrote:
Way to go.
Try doing this.
x = MutableNumeric( 42)
^^^^^^^^^^^^^^
where is this defined?
y = x
x += 42
print y

--
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
Jan 2 '07 #4
Andreas Beyer wrote:
There has been quite some traffic about mutable and immutable data types
on this list. I understand the issues related to mutable numeric data
types. However, in my special case I don't see a better solution to the
problem.
Here is what I am doing:

I am using a third party library that is performing basic numerical
operations (adding, multiplying, etc.) with objects of unknown type. Of
course, the objects must support the numerical operators. In my case the
third party library is a graph algorithm library and the assigned
objects are edge weights. I am using the library to compute node
distances, etc.

I would like to be able to change the edge weights after creating the
edges. Otherwise, I would have to remove the edges and re-create them
with the new values, which is quite costly. Since I also didn't want to
change the code of the graph library, I came up with a mutable numeric
type, which implements all the numerical operators (instances are of
course not hashable). This allows me to change the edge weights after
creating the graph.

I can do the following:
>>x = MutableNumeric( 10)
>>y = MutableNumeric( 2)
>>x*y
20
>>x.value = 1.3
>>x*y
2.6000000000000 001
>>>

The effect of numerical operations is determined by the contained basic
data types:
>>x.value = 3
>>x/2
1
>>x.value = 3.0
>>x/2
1.5
>>>

Augmented operations change the instance itself:
>>x.value = 0
>>id(x)
-1213448500
>>x += 2
>>x
MutableNumeric( 2)
>>id(x) # show that same instance
-1213448500
>>>

Is there anything wrong with such design?
The library you are planning to feed with your mutable numbers has to be
designed with such somewhat unusual beasts in mind. For instance, it can no
longer cache intermediate values as their constituents may have changed
without notification.
Don't use that design unless the library's designers explicitly allow it or
at least after extensive testing. Be aware that in the latter case every
new version of the library may break your app beyond fixability.
I am a bit surprised that
Python does not already come with such data type (which is really simple
to implement).
I'm guessing: Such a type is not normally useful -- and if you need it it is
really simple to implement :-)

Peter

Jan 2 '07 #5
Helmut Jarausch schrieb:
pg******@acay.c om.au wrote:
>Way to go.
Try doing this.
x = MutableNumeric( 42)
^^^^^^^^^^^^^^
where is this defined?
In the OPs example.

Diez
Jan 2 '07 #6

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

Similar topics

17
7402
by: Gordon Airport | last post by:
Has anyone suggested introducing a mutable string type (yes, of course) and distinguishing them from standard strings by the quote type - single or double? As far as I know ' and " are currently interchangeable in all circumstances (as long as they're paired) so there's no overloading to muddy the language. Of course there could be some interesting problems with current code that doesn't make a distinction, but it would be dead easy to fix...
50
6383
by: Dan Perl | last post by:
There is something with initializing mutable class attributes that I am struggling with. I'll use an example to explain: class Father: attr1=None # this is OK attr2= # this is wrong def foo(self, data): self.attr1=data self.attr2.append(data) The initialization of attr1 is obviously OK, all instances of Father redefine it in the method foo. But the initialization of attr2 is wrong
1
1582
by: wtnt | last post by:
Hello. I previously had a program that compiled and worked with no error. Relevant parts here: class BasicList{ public: char* listLookup() { item = buffer; ...
18
2601
by: Markus.Elfring | last post by:
The C++ language specification provides the key word "mutable" that is not available in the C99 standard. Will it be imported to reduce any incompatibilities? http://david.tribble.com/text/cdiffs.htm#C99-cpp-keyword http://www.inf.uni-konstanz.de/~kuehl/c++-faq/const-correctness.html#faq-18.13 Regards, Markus
13
1615
by: Suresh Jeevanandam | last post by:
# I am new to python. In python all numbers are immutable. This means there is one object ( a region in the memory ) created every time we do an numeric operation. I hope there should have been some good reasons why it was designed this way. But why not have mutable numbers also in the language. A type which would behave as follows: a = MutableInt(12)
3
2156
by: Mythran | last post by:
http://msdn2.microsoft.com/en-US/library/ms229057(VS.80).aspx * Do not assign instances of mutable types to read-only fields. I would have to disagree with this "Field Design" guidelines...to an extent. Example: public class SomeClass { private NameValueCollection mCollection;
3
1455
by: Sambo | last post by:
By accident I assigned int to a class member 'count' which was initialized to (empty) string and had no error till I tried to use it as string, obviously. Why was there no error on assignment( near the end ). class Cgroup_info: group_name = "" count = "0" #last time checked and processed/retrieved first = "0" last = "" retrieval_type = "" # allways , ask( if more than some limit), none date_checked = ""
2
4270
by: subramanian100in | last post by:
I am reading David Musser's "STL Tutorial and Reference Guide" Second Edition. In that book, on pages 68-69, definition has been given that "an iterator can be mutable or constant depending on whether the result of operator* is a reference or a constant reference." As per this definition, on page 71 in this book, it is mentioned that for 'set' and 'multiset', both the iterator and const_iterator types are constant bidirectional types -...
24
2526
by: Steven D'Aprano | last post by:
Sometimes it seems that barely a day goes by without some newbie, or not- so-newbie, getting confused by the behaviour of functions with mutable default arguments. No sooner does one thread finally, and painfully, fade away than another one starts up. I suggest that Python should raise warnings.RuntimeWarning (or similar?) when a function is defined with a default argument consisting of a list, dict or set. (This is not meant as an...
0
9519
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
10214
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
10164
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
10001
tracyyun
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9042
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...
0
5437
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
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3723
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
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.