4

I am learning Python, and I am a little confused about the data types of Python. I am reading this line again and again.:

'Everything is an object in Python'

This includes integer, floats, string, sets, lists, etc. and when we write like this: [1, 2, 3], so we are actually creating an object of list class (or not)? So, when we write an integer like 123, does it mean it's an object of int class? But when we read in books it says that to convert a string into integer we need to use the int method?

But what I am thinking is that int is a class that also accepts the string object in constructor and then we get integer object, right?

So, is int a method or class in Python?

Jan Z.
  • 6,883
  • 4
  • 23
  • 27
Vikram Chhipa
  • 113
  • 1
  • 15

2 Answers2

9

Yes int is a class (and it's also called a type; see Python : terminology 'class' VS 'type'), and doing int('123') returns an instance of an int object.

However, (in standard Python aka CPython) for small integers (in the range -5 to 256, inclusive) the int constructor doesn't actually build a new integer object. For efficiency reasons the interpreter has a cache of small integers and the constructor simply returns a reference to the existing int object. This topic is discussed in the answers to “is” operator behaves unexpectedly with integers.

Your book that calls int() "the int method" is being a tiny bit sloppy, IMHO. Pedantically speaking, int itself is a class, which is a callable object, and when you call a class that call gets automatically converted into a call to the class's constructor method (that is, its __new__ method). But informally it's common to refer to int() as a function call or method call.

I almost forgot about the question in your first paragraph. When we write

[1, 2, 3]

the interpreter creates the 3 int objects and puts them inside a fresh list instance. (To be more precise, it puts references to the int objects into the list).

Using the standard dis module you can disassemble the bytecode for this operation:

from dis import dis
dis('a=[1,2,3]')    

output

  1           0 LOAD_CONST               0 (1)
              3 LOAD_CONST               1 (2)
              6 LOAD_CONST               2 (3)
              9 BUILD_LIST               3
             12 STORE_NAME               0 (a)
             15 LOAD_CONST               3 (None)
             18 RETURN_VALUE

So even though we're "just" creating a literal list it's still a fully-fledged list instance object. Unlike some OOP languages, Python doesn't have any "primitive" datatypes that aren't objects, so literal integers and literal strings are also objects. Thus a literal string comes equipped with all the standard string methods. Eg,

print('hello'.lower)

output

built-in method lower of str object at 0xb72e7880>

shows us that the literal string 'hello' has the standard lower() method.

PM 2Ring
  • 54,345
  • 6
  • 82
  • 182
  • Just a minor mistake (according to me) : `the interpreter creates the 3 int objects and puts them inside a fresh list instance.` It does not put the objects in the list, it's the references of the objects that is kept in the list. – Pranjal Kumar Jul 13 '18 at 02:55
  • @PranjalKumar I've updated my answer. I didn't mention references originally because we always work with object references in Python, and discussing that is a topic for another question. ;) – PM 2Ring Jul 13 '18 at 05:07
  • Do you know why the disassembly is different when a literal is used vs a class? For example dis("x = 1000") is different from dis("x = int(1000)") – IKnewThat Sep 17 '20 at 00:44
  • @IKnewThat Why shouldn't it be different? If your code says to call `int(1000)`, then the interpreter does that call & returns the result. Although `int(1000)` normally calls the built-in int constructor, it's possible to "shadow" that built-in name. Eg, consider what happens if you do `int = str; x = int(1000)` – PM 2Ring Sep 17 '20 at 04:08
2

As you can read in the python documentation.

class int(x, base=10) Return an integer object constructed from a number or string x, or return 0 if no arguments are given. If x is a number, it can be a plain integer, a long integer, or a floating point number. If x is floating point, the conversion truncates towards zero. If the argument is outside the integer range, the function returns a long object instead.

So it's a class constructor that constructs an int object, depending on the given input x.

DJanssens
  • 17,849
  • 7
  • 27
  • 42
  • thanks DJanssens :), so when we create datatypes literls actually we are creating the objects of their classes ? ex:- x = [] so x is an list class object ? right , and similer in other datatypes, because that's why we can call method on them ? x.index('aa') ? – Vikram Chhipa Jul 16 '16 at 07:35
  • Yep @vikram, some objects are created that often that they made a literal for it. Because of that you can do x = 3, which will create an int object under the cover. Same goes for lists and any other python type. – DJanssens Jul 16 '16 at 07:38