user9013730
user9013730

Reputation:

Python Data types vs Class?

bool, int, and str are data types in Python.

>>> type(True)
<class 'bool'>
>>> 

>>> type(2019)
<class 'int'>
>>> 

>>> type('Why')
<class 'str'>
>>> 

But when I put these words to Python interpreter, it shows that these are actually a class.

>>> bool
<class 'bool'>
>>> 

>>> int
<class 'int'>
>>> 

>>> str
<class 'str'>
>>> 

I've been searching around but couldn't find an answer about this. Since I can't find the answer on the internet, I guess someone here must have an answer for it.

What is the relation between data types and class? Why bool, int, and str do not produce data types word in Python interpreter?

Upvotes: 2

Views: 352

Answers (3)

picmate 涅
picmate 涅

Reputation: 4249

In Python, everything is an object. 123 is an object, 'Hello' is an object and 4.5 is an object. The content of each of these values let the Python interpreter pick the right class and create the right object for the value. For example, when you enter 123, the Python interpreter will scan the value and create an object of class int. Same goes for booleans.

Classes like int, str, bool etc. are defined in the built-in namespace in Python. Meaning, these classes are available everywhere in a Python environment (not dependent on a module, by default you'll have access to these classes). For example, if you do the following in the Python interpreter:

class SampleClass:
     pass

and call dir()

you'll get a list with all the classes, functions, variables that you defined in the current scope you are in (which is the global scope). For example, that list would look like:

['SampleClass', '__builtin__', '__doc__', ...]

Notice that there are also items like __builtin__ and __doc__ in the list. These are not things that you defined but are available by default. If you go a step further and do:

dir(__builtin__), you will see another list with the built-in classes that are available to you by default. Just like SampleClass was there in the earlier list, you can find all the built-in classes in this list.

['ArithmeticError', 'AssertionError', 'AttributeError',...,'bool', ... 'int', 'isinstance', 'iter']

Now, if you do a dir on int, you will see all the attributes that are in the int class like:

['__abs__', '__add__', '__and__', '__bool__',...]

As you can see here, __add__ is a method in the int class. This is the method that dictates the behavior for the '+' operation. When you do, 3+4, Python calls the __add__ method in the 3 int object to add a 4 and return the value. Because of this feature in Python, you can create your own int type and do something crazy as this:

class MyInt(int):
    def __add__(self, value):
        return self*value

Now, if you create a MyInt object it is basically going to behave like an int because it is a subclass of the int class, but with an overridden __add__ method.

[In] mi = MyInt(5)
[In] mi-2
[Out] 3
[In] mi+5
[Out] 25

Upvotes: 3

Zach Oakes
Zach Oakes

Reputation: 183

Another way to think about it is that everything in Python is an object, thus these are Class objects, i.e. an integer class object or boolean class object; or an object extending/deriving the boolean class.

In fact C level classes in Cython are called Extension Types.

Zach

Upvotes: 1

erkandem
erkandem

Reputation: 136

Even primitives like integer, string and float are classes in python. Those classes hide the underlying (C-code) implementation.

So if you make an integer assignment like

    c = 3 

you are actually

  • instantiating an integer class
  • with the value 3
  • and probably a lot more of which I may or may not know about

Without a explicit type the literal value 3 is assumed to be an integer (i.e. use 3.0 for float)

Upvotes: 0

Related Questions