简体   繁体   English

在python中分配

[英]assignment in python

I know that "variable assignment" in python is in fact a binding / re-bindign of a name (the variable) to an object. 我知道python中的“变量赋值”实际上是一个名称(变量)与对象的绑定/重新绑定。

This brings the question: is it possible to have proper assignment in python, eg make an object equal to another object? 这带来了一个问题:是否可以在python中进行适当的赋值,例如使对象等于另一个对象?

I guess there is no need for that in python: 我猜在python中没有必要:

  1. Inmutable objects cannot be 'assigned to' since they can't be changed 无法将可分配的对象“分配给”,因为它们无法更改

  2. Mutable objects could potentially be assigned to, since they can change, and this could be useful, since you may want to manipulate a copy of dictionary separately from the original one. 可以分配可变对象,因为它们可以更改,这可能很有用,因为您可能希望与原始对象分开处理字典副本。 However, in these cases the python philosophy is to offer a cloning method on the mutable object, so you can bind a copy rather than the original. 但是,在这些情况下,python哲学是在可变对象上提供克隆方法,因此您可以绑定副本而不是原始副本。

So I guess the answer is that there is no assignment in python, the best way to mimic it would be binding to a cloned object 所以我猜答案是python中没有赋值,模仿它的最好方法是绑定到克隆对象

I simply wanted to share the question in case I'm missing something important here 我只想分享这个问题,以防我在这里遗漏一些重要的事情

Thanks 谢谢

EDIT: 编辑:

Both Lie Ryan and Sven Marnach answers are good, I guess the overall answer is a mix of both: Lie Ryan和Sven Marnach的答案都很好,我想整体答案是两者兼而有之:

For user defined types, use the idiom: 对于用户定义的类型,请使用成语:

a. 一种。 dict = dict(b. dict ) dict = dict( b.dict

(I guess this has problems as well if the assigned class has redefined attribute access methods, but lets not be fussy :)) (我想如果指定的类重新定义了属性访问方法,这也有问题,但不要挑剔:))

For mutable built-ins (lists and dicts) use the cloning / copying methods they provide (eg slices, update) 对于可变内置命令(列表和dicts),使用它们提供的克隆/复制方法(例如切片,更新)

finally inmutable built-ins can't be changed so can't be assigned 最后无法更改的内置函数无法更改,因此无法分配

I'll choose Lie Ryan because it's an elegant idiom that I hadn't thought of. 我会选择Lie Ryan,因为这是一个我没想过的优雅成语。

Thanks! 谢谢!

I think you are right with your characterization of assignment in Python -- I just would like to add a different method of cloning and ways of assignment in special cases. 我认为你对Python中赋值的描述是正确的 - 我只是想在特殊情况下添加一种不同的克隆方法和赋值方法。

"Copy-constructing" a mutable built-in Python object will yield a (shallow) copy of that object: “复制构造”一个可变的内置Python对象将产生该对象的(浅)副本:

l = [2, 3]
m = list(l)
l is m
--> False

[ Edit : As pointed out by Paul McGuire in the comments, the behaviour of a "copy contructor" (forgive me the C++ terminology) for a immutable built-in Python object is implementation dependent -- you might get a copy or just the same object. [ 编辑 :正如Paul McGuire在评论中指出的那样,对于不可变的内置Python对象,“复制构造函数”(原谅我的C ++术语)的行为依赖于实现 - 你可能得到一个副本或者只是相同的宾语。 But because the object is immutable anyway, you shouldn't care.] 但是因为对象无论如何都是不可改变的,你不应该在意。]

The copy constructor could be called generically by y = type(x)(x) , but this seems a bit cryptic. 复制构造函数可以通过y = type(x)(x)一般调用,但这看起来有点神秘。 And of course, there is the copy module which allows for shallow and deep copies. 当然,还有copy模块,允许浅和深拷贝。

Some Python objects allow assignment. 一些Python对象允许赋值。 For example, you can assign to a list without creating a new object: 例如,您可以在不创建新对象的情况下分配给列表:

l = [2, 3]
m = l
l[:] = [3, 4, 5]
m
--> [3, 4, 5]

For dictionaries, you could use the clear() method followed by update(otherdict) to assign to a dictionary without creating a new object. 对于字典,您可以使用clear()方法,然后使用update(otherdict)分配给字典而不创建新对象。 For a set s , you can use 对于一组s ,你可以使用

s.clear()
s |= otherset

This brings the question: is it possible to have proper assignment in python, eg make an object equal to another object? 这带来了一个问题:是否可以在python中进行适当的赋值,例如使对象等于另一个对象?

Yes you can: 是的你可以:

a.__dict__ = dict(b.__dict__)

will do the default assignment semantic in C/C++ (ie do a shallow assignment). 将在C / C ++中执行默认赋值语义(即执行浅层赋值)。

The problem with such generalized assignment is that it never works for everybody. 这种广义分配的问题在于它永远不适合所有人。 In C++, you can override the assignment operator since you always have to pick whether you want a fully shallow assignment, fully deep assignment, or any shade between fully deep copy and fully shallow copy. 在C ++中,您可以覆盖赋值运算符,因为您总是需要选择是否需要完全浅层赋值,完全深度赋值或完全深度复制和完全浅复制之间的任何阴影。

I don't think you are missing anything. 我不认为你错过任何东西。

I like to picture variables in python as the name written on 'labels' that are attached to boxes but can change its placement by assignment, whereas in other languages, assignment changes the box's contents (and the assignment operator can be overloaded). 我喜欢将python中的变量描述为附加到框的“标签”上的名称,但可以通过赋值更改其位置,而在其他语言中,赋值会更改框的内容(并且赋值运算符可能会被重载)。

Beginners can write quite complex applications without being aware of that, but they are usually messy programs. 初学者可以编写相当复杂的应用程序,而不会意识到这一点,但它们通常是凌乱的程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM