1

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:

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

Community
  • 1
  • 1
Pat
  • 16,515
  • 15
  • 95
  • 114
  • 1
    For value/reference compare semantics: http://stackoverflow.com/questions/132988/is-there-a-difference-between-and-is-in-python – Andrew Barrett May 04 '12 at 17:13
  • Each object has a unique id. `a is b` is equivalent to `id(a) == id(b)`. Equality comparisons default to this when no `__eq__` or `__cmp__` methods are defined. – Joel Cornett May 04 '12 at 22:57

2 Answers2

3

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.

Joel Cornett
  • 24,192
  • 9
  • 66
  • 88
  • 1
    An object with no `__cmp__` or `__eq__` inherits `object`s comparison, which compares by identity. `object`s `__hash__` implementation also uses identity, so objects are hashable by default. `copy.deepcopy` is when you want to copy a hierarchy of objects, not just an object. `copy` does that and is equivalently controllable by `__copy__`. Your answer isn't wrong but it's terribly incomplete and somewhat misleading. – agf May 04 '12 at 17:27
  • you're right. I should have checked the docs before posting. will edit when i get near a computer. – Joel Cornett May 04 '12 at 17:49
1

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.

jadkik94
  • 7,000
  • 2
  • 30
  • 39