The Python Oracle

What is the value of a class?

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Over a Mysterious Island

--

Chapters
00:00 What Is The Value Of A Class?
00:39 Accepted Answer Score 4
01:22 Answer 2 Score 3
02:20 Answer 3 Score 0
03:47 Answer 4 Score 1
04:31 Thank you

--

Full question
https://stackoverflow.com/questions/4272...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python

#avk47



ACCEPTED ANSWER

Score 4


The Python language reference doesn't bother to define the concept of "value", leaving it a fuzzy, intuitive notion. Trying to apply the concept beyond the point where it becomes unintuitive doesn't work well, and the Python language reference itself doesn't try.

If you really want to pick something to call the "value" of Foo, you could say the value is "a user-defined type with base class object and one method, __init__, that takes one argument named a and assigns it to the a attribute of self", but this won't be useful as part of your conceptual model or as a tool for communicating with other people.




ANSWER 2

Score 3


I believe the usual meaning of the "value" of an object is enough of its attributes to determine whether it compares equal to another object of the same type:

>>> x = {1, 2, 3}
>>> y = {1, 2, 3}
>>> id(x) == id(y)
False
>>> x == y
True

In this example, the sets x and y have the same value and same class but different identities.

>>> class Foo:
...     def __eq__(self, other):
...         return id(self) == id(other)
... 
>>> f = Foo()
>>> f.x = 1
>>> g = Foo()
>>> g.x = 1
>>> f == g
False
>>> h = f
>>> id(h) == id(f)
True
>>> h == f
True

In this case, f and g have the same type but different identities and different values (even though all their attributes are the same). f and h have the same identity and value.

(Incidentally we have have made the __eq__ method simply return False always, so that h and f - or for that matter f and f - have the same identity but different values, but I imagine such a broken equality definition would break in some places in practice. Equality should be an equivalence relation.)




ANSWER 3

Score 1


The reference manual notes, somewhat obliquely, that value is a notion for each type to define for itself (emphasis added):

An object’s type determines the operations that the object supports (e.g., “does it have a length?”) and also defines the possible values for objects of that type.

In other words, there is no general notion of what "a value" is. Each type defines for itself what "value" means for objects of that type. Usually, the notion of "value" is linked to the notion of equality; objects are considered to have "the same value" if the compare equal. So a type can specify its notion of value by defining an __eq__ method.

So, to answer your specific question about classes: In this sense, the value of a class object is just itself, since class objects are compared by object identity, and no two class objects are equal.




ANSWER 4

Score 0


I contend that the notion of value has a meaning only if we are able to compare it against another object. This claim is supported by the official Python 3 documentation:

Chapter Objects, values and types states that objects have a value (in addition to type and identity). The value of an object is a rather abstract notion in Python: For example, there is no canonical access method for an object’s value. Also, there is no requirement that the value of an object should be constructed in a particular way, e.g. comprised of all its data attributes. Comparison operators implement a particular notion of what the value of an object is. One can think of them as defining the value of an object indirectly, by means of their comparison implementation.

The notion of value is even orthogonal to the type:

>>> x = 1
>>> y = 1.0
>>> x == y
True         # Therefore x and y have the same value, although of different types

Thus, if a value of an object can be examined only through equality testing, then we must admit that the value of a class object is indistinguishable from its identity. Proof:

>>> def make_class():
...     class A: pass
...     return A
... 
>>> a1 = make_class()
>>> a2 = make_class()
>>> id(a1) == id(a2)
False
>>> a1 == a2
False

In the above example, the function make_class() creates a new class object on every call. Semantically, those class objects should be identical (even if they are defined not to be empty). Still objects from different invocations of the make_class() function do not compare equal, therefore they are assumed to have non-equal values merely because they have different identities.