简体   繁体   中英

What do you call the term when you set the variable to a method?

from tkinter import *              
root = Tk()        
button1 = Button(root, text= "Hello world click here to close")   
y = button1.pack()         
print (type (y))      
root.mainloop()

When setting y to button1.pack() , what do you call that technique and why does it return "<class 'NoneType'>"?

y = button1.pack() sets y to an expected returned value from the pack() method.

For example,

foo = random.randint() sets foo to the random number returned by the randint method.

foo = random.randint , however, assigns the randint method to the variable foo

Since this method does not return anything, y has no value, and type() indicates as such

What you want to do is use y = button1.pack , which will assign that function to y

If you run type() on y after that, you will see that it returns it's type as "function"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM