可变数字 [英] Mutable numbers

查看:61
本文介绍了可变数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#我是python的新手。


在python中,所有数字都是不可变的。这意味着每次进行数值运算时都会创建一个对象(内存中的
区域)。我希望应该有一些很好的理由为什么它是这样设计的。


但是为什么语言中也没有可变数字。一种类型

的行为如下:


a = MutableInt(12)

b = a

现在a和b都应该引用相同的内存位置。对象的任何变化都应该反映在所有引用中。


a.assign(13)#相同的内存位置应该用值13更新, b

现在也是13。


现在我们可以进一步优化:


a.incrementOne()#等同于C ++中的++

a.decrementOne()


和增强的赋值操作也可以进行优化。


在任何应用中,大多数操作都是数字的。所以,我认为,我们

应该可以通过可变的

数字获得良好的速度优势。您怎么看?


问候,

Suresh

解决方案

Suresh Jeevanandam写道:

但为什么语言中也没有可变数字。


我给出的简短回答可能就是用一个

用户定义的类很容易做到这一点从来没有过所有这一切。

在任何应用程序中,大部分操作都是数字化的。所以,我认为,我们应该通过可变数量的可用性获得良好的速度优势。您怎么看?




为什么您认为这会带来任何速度优势?数学运算在Python中可能很慢的原因是因为它的解释是b / b
。有可变的数字并没有改变这个事实。


-

Erik Max Francis&& ma*@alcyone.com && http://www.alcyone.com/max/

美国加利福尼亚州圣何塞市&& 37 20 N 121 53 W&& AIM erikmaxfrancis

我将永远记住/这一刻

- Sade


Suresh Jeevanandam写道:

在python中,所有数字都是不可变的。


True。

这意味着每次我们做数字时都会创建一个对象(
内存中的一个区域)
操作。


错误。存储区域可以以分配的方式合并。是一个O(1)

的操作。

我希望应该有一些很好的理由为什么它是这样设计的。


是的,这样您就可以获得数值类型的按值传递语义。那个

,如果你把一个数字传递给一个函数,它就不能修改它。你坚持使用

" a = foo(a)"对a进行计算,而不是让半程序执行

" foo(a)"而另一半做a = foo(a)。此外,这样做是为了你

不需要特殊情况整数文字:它们是(固定)名称相同的

不可变实例和没有人可以修改13或任何其他文字的值。

但为什么语言中也没有可变数字。一种类型的行为如下:

a = MutableInt(12)
b = a

现在a和b都应该引用相同的记忆位置。对象中的任何更改都应该反映在所有引用中。

a.assign(13)#相同的内存位置应该更新为值13,b
也是13现在。

现在我们可以进一步优化:a.incrementOne()#等效于C ++中的++
a.decrementOne()


所以你刚刚创建了一个不能与普通算子一起使用的数字类型。

这是非常单调的。我认为没有人想要写incrementOne。当

" + 1"存在并且和小学过去一样。即使你把
超载了所有的操作员,你仍然无法使用''=''来完成

的任务,这几乎让你的班级无用或非常unpythonic

使用(=错误)。

在任何应用程序中,大多数操作都是数字的。所以,我认为,我们应该通过可变数量的可用性获得良好的速度优势。您怎么看?



您正在进行过早优化。它还为时过早,甚至是一个大脑

的东西。你*假设* Python很慢,你*假设*这推测

缓慢来自数字是不可变的。但是你没有*测量*它,你是否b / b
并没有在现实世界的情况下尝试它。您甚至没有查看Python源代码来检查您的假设是否正确。在设计不存在的问题的解决方案之前,请尝试检查您的假设

。我建议你在做这么多假设之前开始编写现实世界的Python。

-

Giovanni Bajo


Suresh Jeevanandamaécrit:

#我是python的新手。

在python中,所有数字都是不可变的。这意味着每次进行数值运算时都会创建一个对象(内存中的一个区域)。我希望应该有一些很好的理由为什么它是这样设计的。




优化整数的内存分配。 ''小'整数

(-5到100 IIRC之间)被分配一次并重复使用。

更大整数的内存被分配一次并尽可能重用,所以

malloc()开销可以忽略不计。


# 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

解决方案

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 "incrementOne" 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.


这篇关于可变数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆