# 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)
b = a
Now both a and b should refer to the same memory location. Any change in
the object should get reflected in all the references.
a.assign(13) # Same memory location should be updated with value 13, b
is also 13 now.
Now we can further optimize:
a.incrementOne( ) # equivalent to a++ in C++
a.decrementOne( )
and the augmented assignment operation also could be made optimized.
In any application most of the operation is numerical. So, i think, we
should get a good speed advantage with the availability of mutable
numbers. What do you think ?
regards,
Suresh 13 1618
Suresh Jeevanandam wrote: But why not have mutable numbers also in the language.
The short answer I'd give is probably that this is so easy to do with a
user-defined class that it's never been all that pressing.
In any application most of the operation is numerical. So, i think, we should get a good speed advantage with the availability of mutable numbers. What do you think ?
Why do you think this would afford any speed advantage at all? The
reason that math operations are potential slow in Python is because it's
interpreted; having mutable numerics doesn't change that fact.
--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
I will always remember / This moment
-- Sade
Suresh Jeevanandam wrote: In python all numbers are immutable.
True.
This means there is one object ( a region in the memory ) created every time we do an numeric operation.
False. Memory regions can be pooled in a way that "allocation " is a O(1)
operation.
I hope there should have been some good reasons why it was designed this way.
Yes, so that you can obtain a pass-by-value semantic for numeric types. That
is, if you pass a number to a function, it can't modify it. You consistenly use
"a = foo(a)" to do a calculation on "a", instead of having half programs doing
"foo(a)" and the other half doing "a = foo(a)". Also, it's done so that you
don't need to special case integer literals: they are (fixed) names to the same
immutable instances and nobody can modify the value of 13 or any other literal.
But why not have mutable numbers also in the language. A type which would behave as follows:
a = MutableInt(12) b = a
Now both a and b should refer to the same memory location. Any change in the object should get reflected in all the references.
a.assign(13) # Same memory location should be updated with value 13, b is also 13 now.
Now we can further optimize:
a.incrementOne( ) # equivalent to a++ in C++ a.decrementOne( )
and the augmented assignment operation also could be made optimized.
So you just created a numeric type which does not work with normal operators.
This is so unpythonic. I don't think anybody wants to write "incrementO ne" when
"+ 1" exists and does the same it used to do in primary school. Even if you
overloaded all the operators, you still wouldn't be able to use '=' to do
assignment, which pretty much makes your class useless or very unpythonic to
use (= wrong).
In any application most of the operation is numerical. So, i think, we should get a good speed advantage with the availability of mutable numbers. What do you think ?
You are doing premature optimization. It's so premature that it's even a brain
thing. You *presume* Python to be slow, you *presume* that this presumed
slowness comes from numbers being immutables. But you didn't *measure* it, you
didn't try it on a real-world case. You didn't even look at the Python source
code to check if your assumptions were true. Try to check your assumptions
before designing solutions to problems which do not exist. I suggest you start
writing real-world Python before doing so many assumptions.
--
Giovanni Bajo
Suresh Jeevanandam a écrit : # 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.
The memory allocation for integers is optimized. 'Small' integers
(between -5 and 100 IIRC) are allocated once and reused. The memory for
larger integers is allocated once and reused whenever possible, so the
malloc() overhead is negligible.
fraca7 wrote: Suresh Jeevanandam a écrit :
# 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.
The memory allocation for integers is optimized. 'Small' integers (between -5 and 100 IIRC) are allocated once and reused. The memory for larger integers is allocated once and reused whenever possible, so the malloc() overhead is negligible.
The first bit's right, the second bit isn't: id(12100)
4604168 id(121*100)
4604204
regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/
fraca7 wrote: Suresh Jeevanandam a écrit :
# 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.
The memory allocation for integers is optimized. 'Small' integers (between -5 and 100 IIRC) are allocated once and reused. The memory for larger integers is allocated once and reused whenever possible, so the malloc() overhead is negligible.
[Thinks: or maybe fraca7 just meant that integers will be garbage
collected when there are no more references to them].
regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/
Steve Holden wrote: The memory allocation for integers is optimized. 'Small' integers (between -5 and 100 IIRC) are allocated once and reused. The memory for larger integers is allocated once and reused whenever possible, so the malloc() overhead is negligible.
The first bit's right, the second bit isn't:
>>> id(12100) 4604168 >>> id(121*100) 4604204 >>>
regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/
-- http://mail.python.org/mailman/listinfo/python-list
Steve Holden wrote: The memory allocation for integers is optimized. 'Small' integers (between -5 and 100 IIRC) are allocated once and reused. The memory for larger integers is allocated once and reused whenever possible, so the malloc() overhead is negligible.
The first bit's right, the second bit isn't:
>>> id(12100) 4604168 >>> id(121*100) 4604204 >>>
quoting from Objects/intobject.c :
Integers are quite normal objects, to make object handling uniform.
(Using odd pointers to represent integers would save much space
but require extra checks for this special case throughout the code.)
Since a typical Python program spends much of its time allocating
and deallocating integers, these operations should be very fast.
Therefore we use a dedicated allocation scheme with a much lower
overhead (in space and time) than straight malloc(): a simple
dedicated free list, filled when necessary with memory from malloc(). http://svn.python.org/projects/pytho...ts/intobject.c
</F>
On Tue, 21 Feb 2006 12:44:52 +0530 in comp.lang.pytho n, Suresh
Jeevanandam <jm*******@gmai l.com> wrote: # 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)
a = [12]
b = a
Now both a and b should refer to the same memory location. Any change in the object should get reflected in all the references.
a.assign(13) # Same memory location should be updated with value 13, b
a[0] = 13
is also 13 now.
Now we can further optimize:
a.incrementOne () # equivalent to a++ in C++ a.decrementOne ()
a[0] += 1
a[0] -= 1 and the augmented assignment operation also could be made optimized.
In any application most of the operation is numerical. So, i think, we should get a good speed advantage with the availability of mutable numbers. What do you think ?
I don't see how.
HTH,
-=Dave
--
Change is inevitable, progress is not.
Steve Holden a écrit : [Thinks: or maybe fraca7 just meant that integers will be garbage collected when there are no more references to them].
Actually I meant that the memory is reused, but the same integer won't
always have the same address.
I guess that in your example, the '121' is assigned the 'old' address of
12100, and the result of the operation is assigned the next chunk; the
following seems to confirm that: id(12100)
8628480 id(102)
8628480 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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
|
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
|
by: Kjetil Kristoffer Solberg |
last post by:
What is a mutable struct?
regards
Kjetil Kristoffer Solberg
|
by: Russ |
last post by:
I'd like to get output formatting for my own classes that mimics the
built-in output formatting. For example,
>>> x = 4.54
>>> print "%4.2f" % x
4.54
In other words, if I substitute a class instance for "x" above, I'd
like to make the format string apply to an element or elements of the
instance. Can I somehow overload the "%" operator for that? Thanks.
| |
by: Water Cooler v2 |
last post by:
Are JavaScript strings mutable? How're they implemented -
1. char arrays
2. linked lists of char arrays
3. another data structure
I see that the + operator is overloaded for the string class and hence
it is possible to do:
txt += "foo bar";
|
by: Andreas Beyer |
last post by:
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...
|
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 -...
|
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...
|
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,...
|
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...
| |
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...
|
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,...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |