How to use a variable as function name in Python

前端 未结 6 1935
庸人自扰
庸人自扰 2020-12-01 16:17

Would it be possible to use a variable as a function name in python? For example:

list = [one, two, three]
for item in list:
    def item():
         some_st         


        
6条回答
  •  渐次进展
    2020-12-01 17:20

    The short answer is no. When you declare a variable, you have bound a name to an object. The same is true when you declare a function. You can try it out for yourself in a python console and see what happens:

    >name=1
    >name
    1
    >def name(x): print(x+1)
    
    >name
    function name at 0x000001CE8B8122F0
    

提交回复
热议问题