What does it mean when you assign int to a variable in Python?

i.e. x = int

I understand that an x

integer will do it if there isn't one already, but I would like to understand the process behind it. In particular, I would like to know what int

(as opposed to int()

). I know what int()

is a function, but I'm not sure what it is int

. Documentation links about int

would be helpful as I couldn't find them.

+3


source to share


2 answers


Imagine you had a function named func

def func():
    print("hello from func")
    return 7

      

If you then assign func

- x

, you assign to the function itself x

not the result of the call



x = func # note: no ()
x() # calls func()
y = x() # y is now 7

      

In this context, you are looking at a very similar thing with int

.

x = int
y = x('2') # y is now 2

      

+6


source


x = int

will not convert x

to an integer. int

- integer type. Execution x = int

sets the x

value of the type int

. In short, it x

becomes an "alias" for an integer type.

If you call the int type on something, for example int('2')

, it will convert what you give to an integer if possible. If you assign the result of this call to a variable, it will set that variable to the integer value you received when you called int

. Therefore, the installation will x = int('2')

set x

to 2.



You should read the Python tutorial to understand how types, variables, and calls work in Python.

+7


source







All Articles