Python对象与.NET对象的比较 [英] Python objects compared to .NET objects

查看:32
本文介绍了Python对象与.NET对象的比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对.NET对象及其引用和值类型的框架非常满意.python对象与.NET对象相比如何?具体来说,我想知道等式 obj1 == obj2 ,哈希功能(即能够放入字典)和复制.

I'm very comfortable with .NET objects and its framework for reference vs value types. How do python objects compare to .NET objects? Specifically, I'm wondering about equality obj1 == obj2, hash-ability (i.e. being able to put in a dict), and copying.

例如,在.NET中,默认情况下,所有对象都是引用类型,其相等性和哈希码由其在内存中的地址确定.此外,将变量分配给现有对象只会使其指向内存中的该地址,因此不会发生昂贵的复制.似乎对于python来说是相同的,但是我不确定.

For instance, by default in .NET, all objects are reference types and their equality and hash code is determined by their address in memory. Additionally, assigning a variable to an existing object just makes it point to that address in memory, so there is no expensive copying occurring. It seems that this is the same for python, but I'm not entirely sure.

  • 平等
    • Equality
      • is checks for referential equality, == checks for value equality (but what does value equality mean for objects?)

      我能够从effbot中找到一些有用的信息:

      I was able to find some useful info from the effbot written back in 2000:

      对象

      所有Python对象都具有以下内容:

      Objects

      All Python objects have this:

      • 唯一身份(整数,由 id(x)返回)
      • 一种类型(由 type(x)返回)
      • 某些内容,您无法更改身份.

      您不能更改类型.

      某些对象允许您更改其内容(无需更改身份或类型).

      Some objects allow you to change their content (without changing the identity or the type, that is).

      某些对象不允许您更改它们的内容(更多内容在下面).

      Some objects don’t allow you to change their content (more below).

      该类型由类型对象表示,该对象了解更多有关这种类型的对象(它们通常占用多少字节的内存,他们有什么方法,等等.

      The type is represented by a type object, which knows more about objects of this type (how many bytes of memory they usually occupy, what methods they have, etc).

      推荐答案

      平等

      :-没有定义 __ cmp __ __ eq __ 方法的对象,如果您尝试进行比较,则会引发错误,该继承自对象对象.这意味着执行 a>b 等同于执行 id(a)>id(b).

      Equality

      : - An object with no __cmp__ or __eq__ method defined will raise an error if you try to compare it inherits its comparisons from the object object. This means that doing a > b is equivalent to doing id(a) > id(b).

      is 关键字还用于查看两个变量是否指向同一对象.另一方面, == 运算符调用要比较的对象的 __ cmp __ __ eq __ 方法.

      The is keyword is also used to see if two variables point to the same object. The == operator on the other hand, calls the __cmp__ or __eq__ method of the object it is comparing.

      可哈希性

      :-如果为对象定义了 __ hash __ 方法,则该对象是可哈希的.所有基本数据类型(包括字符串和元组)都为它们定义了哈希函数.如果没有为类定义 __ hash __ 方法,则该类将从对象 object 继承其哈希.

      Hashability

      : - An object is hashable if there is a __hash__ method defined for it. All of the basic data types (including strings and tuples) have hash functions defined for them. If there is no __hash__ method defined for a class, that class will inherit it's hash from the object object.

      复制

      :- copy copy.deepcopy 以及它们各自的类方法 __ copy __ __ deepcopy __ .使用 copy 复制单个对象,使用 deepcopy 复制对象的层次结构. deepcopy

      Copying

      : - copy, copy.deepcopy, and their respective in class methods __copy__ and __deepcopy__. Use copy to copy a single object, deepcopy to copy a heirarchy of objects. deepcopy

      根据agf的建议进行的编辑.

      Edits made at the suggestion of agf.

      这篇关于Python对象与.NET对象的比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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