473,394 Members | 1,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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.6000000000000001
>>>
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 1575
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(args):
x = _gw(args)
do_something_to(x)
return x

GraphLibrary.GraphWalker = 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.com.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.6000000000000001
>>>

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.com.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
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...
50
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...
1
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
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?...
13
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...
3
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...
3
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...
2
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...
24
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
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...
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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,...
0
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...
0
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...

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.