Everything is an object in python - object class and type class
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Tue Jun 2 02:30:17 EDT 2015
On Tuesday 02 June 2015 11:02, TheDoctor wrote:
> On Monday, June 1, 2015 at 7:33:11 PM UTC-5, Chris Angelico wrote:
>> On Tue, Jun 2, 2015 at 10:24 AM, TheDoctor <dreamingforward at gmail.com>
>> wrote:
>> > A type is not an object in the same way an instantiated type is an
>> > object -- anymore than a blueprint for a building is the building
>> > itself.
>>
>> And don't let dreamingforward confuse you, because he is flat-out
>> wrong. An integer is an object, because you can manipulate it as one.
>
> An integer in Python *acts* like an object, in some narrow sense can
> indeed be said to BE one.
No, they *really are* objects. You can look at the source code of the Python
interpreter to see how ints are implemented, and they are implemented as
objects. You can see that they are bigger than a machine int:
py> sys.getsizeof(23)
14
That's 14 bytes. A machine int is typically 2 bytes (int16), 4 bytes (int32)
or 8 bytes (int64).
If you use ctypes, I expect that you can investigate the internal structure
of the int. If not in ctypes, then you can certainly do so from a C
extension.
> But no one uses them like that in practice
Of course they do. ints have methods:
py> n = 23
py> n.to_bytes(5, "big")
b'\x00\x00\x00\x00\x17'
Is that object oriented enough for you?
--
Steve
More information about the Python-list
mailing list