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

Ned Batchelder ned at nedbatchelder.com
Tue Jun 2 14:10:43 EDT 2015


On Tuesday, June 2, 2015 at 1:59:37 PM UTC-4, BartC wrote:
> On 02/06/2015 18:00, Steven D'Aprano wrote:
> > On Tue, 2 Jun 2015 10:49 pm, BartC wrote:
> >
> >> On 02/06/2015 12:27, Steven D'Aprano wrote:
> 
> >>> In the programming language Java, *some* values are objects, and some
> >>> values are not objects.
> >>>
> >>> In the programming language Python, *all* values are objects, in the
> >>> general sense. That is what we mean by "Everything is an object".
> >>
> >> In a dynamically typed language such as Python, you need to be able to
> >> deal with values consistently whatever their type, simply because you
> >> can't tell what the types are from source code. (Not without a lot of
> >> work which Python doesn't attempt, although RPython might do so.) Example:
> >>
> >>    print (a)
> >>
> >> a might be the int 42, or a it might be a million-element list. So both
> >> are wrapped up as 'objects'.
> >
> > Again, this is not relevant. Javascript is dynamically typed, but some
> > values are machine primitives and other values are objects. The interpreter
> > keeps track of this at runtime.
> 
> 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'

--Ned.

> 
> -- 
> Bartc




More information about the Python-list mailing list