search

Home  >  Q&A  >  body text

python - def自定义函数的疑惑

def change(n):
    n = 'Mr.Gumby'
name = 'Mrs.Entity'
change(name)

这个例子,没太看懂def定义的函数起到了什么作用?
还有一例:

def change(n)
    n[0] = 'Mr.Gumby'
name = ['Mrs.Entity','Mrs.Thing']
change(name)

求解释一下自定义函数起到的作用?
自定义的函数在这个过程中做了什么?怎么做的?

怪我咯怪我咯2781 days ago780

reply all(3)I'll reply

  • PHP中文网

    PHP中文网2017-04-18 10:22:28

    The method you wrote cannot do anything;
    You need to return

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 10:22:28

    Custom function is to encapsulate many things to be done into a method that you can call anywhere.

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 10:22:28

    You don’t know the function of the above function. In fact, it is largely because the function structure above is too simple and has no return value. You can look at slightly more complex functions to understand the construction and calling methods of the function, and you will understand. It's a function. For example, in your second method above, if you need to modify the m-th element in the name list, write this function as a function, and then attach the list and index variables when calling it. You can modify any list through this function. Elements at any position, imagine that the same operation needs to be performed several times at different locations in the code, and just call this function there.

    reply
    0
  • Cancelreply