Python objects compared to .NET objects

故事扮演 提交于 2021-02-05 08:12:13

问题


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.

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.

EDITS:

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

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

Objects

All Python objects have this:

  • a unique identity (an integer, returned by id(x))
  • a type (returned by type(x))
  • some content You cannot change the identity.

You cannot change the type.

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).


回答1:


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).

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.

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.

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

Edits made at the suggestion of agf.




回答2:


In python you can define the __eq__ method to handle the == operator.

The is operator checks if one object is the same as the other. (or more specifically two variables that reference one or two objects)

>>> a = [1, 2, 3]
>>> b = [1, 2, 3]
>>> a == b
True
>>> a is b
False
>>> c = a
>>> c is a
True

Now, this example uses the list type, which you can think of as a class which defines an __eq__ method that compares equality of all its items.

Same for hash, you define a __hash__ method in your class that returns an integer that would identify your object. This is also available in the basic types that support hashing. See the hash function.



来源:https://stackoverflow.com/questions/10453224/python-objects-compared-to-net-objects

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!