Official definition of call-by-value (Re: Finding the instance reference...)
Terry Reedy
tjreedy at udel.edu
Fri Nov 14 22:56:52 EST 2008
rurpy at yahoo.com wrote:
> On Nov 13, 4:53 pm, Terry Reedy wrote:
>> rurpy at yahoo.com wrote:
>>
>>> I have yet to see any reasonable definition of a Python
>>> value in the Python docs or elsewhere, despite the fact
>>> that a value is one of the three defining characteristics
>>> of an object, a central concept in Python.
>> I noticed too. My try:
>>
>> The value of an object is the information that the object represents (or
>> that is stored with the object) that the interpreter uses to compute the
>> value of a new object when you use the object in an expression. For
>> number objects, the number value. For collection objects, the objects
>> collected. For functions, the signature and function performed when called.
>>
>> How is that?
>
> I am starting with the idea that "value" is what we call
> whatever it is that is the difference between, for example,
> the objects int(3) and int(4). While your definition
> seems to be saying something similar it does not seem
> very precise.
I think necessarily so, or rather, it can only be specific for each
class. In the formulation: an object has identify, class, and value,
class and value are separated. Others have said that type/class is a
universe of possible values and operations on those values. Each
instance has a particular value. 'Universe of possible values' is vague
until one gets specific. For some classes, the possible values are
rather complex.
> How would I use your definition to answer the following
> questions?
>
> * How does an object get a value?
A Python interpreter, human or electronic, creates objects with values
as directed by Python code. How it does so is its private secret ;-).
The directive code includes literals, expressions, and some statements.
> * Can I create an object that has a value that
> is the same as int(3) without somehow using an
> int(3) object in its construction?
Yes: 1 + 2
Yes: mpz(3) where mpz is multi-precision int class with same set of
possible values as Python ints.
??? 3.0
??? Fraction(3,1)
While Python sees these as *equal*, one could say they are not the same
because they indicate members of different (non-isomorphic) universes.
> * Do all objects have values? (Ignore the Python
> docs if necessary.)
If one allows null values, I am current thinking yes.
Still, numbers, characters, and collections thereof of what code is
usually about.
> * What is the value of object()?
Essentially none, other than bool(object()) == True.
Ditto for None, other than bool(None) == False.
Otherwise, None and object convey no information.
> * Does an object's behavior (methods) affect
> its value?
My first answer is No. Instance methods are attributes of a class and,
in most cases, the value of a class. In those cases in which the class
of an object can be and is changed to another class, the interpretation
of the value/info of the instance could change and one might claim that
the effective value and hence the value of the object has changed and
hence the answer could be Yes. But this is extremely rarely done and I
could claim that this is a shortcut for creating a new object and
deleting the old.
> * If two objects have different unrelated types
> (i.e. not subclassed), are their values necessarily
> different (regardless of how they print, or act
> when used in an expression)?
No. See above Q. about int(3).
> * What if they aren't unrelated but one is a sub-
> class of the other?
Easier no than above.
> * Can one completely hide an object's value by
> overriding methods?
You tell me ;-)
Do you have an example that you were thinking of?
> * Can two objects have the same value, even if the
> methods of one have been overridden to hide it's
> value as completely as possible?
>
> I wonder if all values come, directly or indirectly,
> from "valued objects" created in the C-API?
CPython creates objects with values in C functions. I presume most are
exposed in the API. But my definition is intentionally not specific to
any implementation.
> Or perhaps value is some sort of useful but fundamentally
> undefinable concept
Either yes or defined in terms of information or universes.
> that disappears when looked at too closely
No.
> Leaving behind only state and behavior?
State is a choice amoung possible states, which is to say, information.
Behavior is determined by class,
Terry Jan Reedy
More information about the Python-list
mailing list