Everything is an object in python - object class and type class

Ian Kelly ian.g.kelly at gmail.com
Tue Jun 2 15:36:16 EDT 2015


On Tue, Jun 2, 2015 at 12:10 PM, Ned Batchelder <ned at nedbatchelder.com> wrote:
> On Tuesday, June 2, 2015 at 1:59:37 PM UTC-4, BartC wrote:
>> Javascript primitives include Number and String.
>>
>> What does Python allow to be done with its Number (int, etc) and String
>> types that can't be done with their Javascript counterparts, that makes
>> /them/ objects?
>
> They have methods (not many, but a few):
>
>>>> i, f = 1000001, 2.5
>>>> i.bit_length()
> 20
>>>> i.to_bytes(6, "big")
> b'\x00\x00\x00\x0fBA'
>>>> f.as_integer_ratio()
> (5, 2)
>>>> f.hex()
> '0x1.4000000000000p+1'

To add a wrinkle to this discussion, Javascript numbers also have methods:

js> (24).toExponential(3)
"2.400e+1"

I believe this is accomplished by implicitly boxing the number in the
Number class when a method or property is accessed. This can be seen
with:

js> (24).toSource()
"(new Number(24))"

Note that "24" and "new Number(24)" are not equivalent.

js> 24 === 24
true
js> 24 === new Number(24)
false
js> typeof(24)
"number"
js> typeof(new Number(24))
"object"

But this is a bit of an implementation detail. So what distinguishes
Javascript primitives from objects? Steven listed "identity" as a
third property of objects upthread; that seems applicable here.



More information about the Python-list mailing list