Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

i.e. x = int

I understand that this will make x an integer if it is not already one, but I'd like to understand the process behind this. In particular, I'd like to know what int is (as opposed to int()). I know that int() is a function, but I'm not sure what int is. Links to documentation about int would be helpful since I couldn't find any.

like image 604
Queen Code Avatar asked Sep 02 '25 05:09

Queen Code


1 Answers

Imagine you had a function called func

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

If you then assigned func to x you are assigning the function itself to x not the result of the call

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

You're looking at a very similar thing with int in this context.

x = int
y = x('2') # y is now 2
like image 125
Ryan Haining Avatar answered Sep 04 '25 18:09

Ryan Haining



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!