Python:在函数名称中使用空格? [英] Python: Use spaces in a function name?

查看:301
本文介绍了Python:在函数名称中使用空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



正常情况下函数被调用:



<$ p $ def myCall():
printHello World

但我想命名/使用该函数为:

  def我的Call():
printI做到了!

我知道世界会开始思考为什么程序员用这种方式命名函数。只需将空格替换为低于分数或其他内容即可!嗯......但这并不是我希望脚本能够工作的方式。



有没有建议用空格来调用函数?



----------------------------------------- --add-ON ----------------------------------------



好吧,伙计们!我将解释我的脚本如何工作。具体为什么我在函数名称中使用空格。在这个页面中有很多的好奇心,因此我将这个附加组件解释了为什么我这样做。



我希望这能帮助大家理解为什么我这么做:)



加入尊重!



例如

  =============== 
欢迎菜单
============== =
1.方案1
2.方案2
3.方案3
4.方案4

我有一个用户主菜单,在显示上面的选项之前,主要会不断更新每一个检查。

  array = [选项1,选项2,选项3,选项4] 

完成检查是因为当某个变量(在脚本前声明)缺失时,具体的选项确认将不起作用。



例如

 对于范围内的x(a)
print菜单选项

我把菜单放在一个循环中(意思只打印一次),m中的数字的计数enu与字符串选项分开时,每当数组中有1个元素打印计数时,都会自动递增。
Option变量放在数组中。



用户将在选项中选择他/她想要的内容。假设用户选择选项1,它将进入阵列(阵列= [选项1,选项2,选项3,选项4])以检查哪个位置。

记住数组不是固定的!它的变化取决于脚本前面的变量声明。



所有的升级都是为了防止错误,崩溃等等。 b
$ b

最后,我写了一个函数(带空格)

然后在循环中调用函数(用空格) 。该函数将需要链接回数组进行更改。



------------------- - - - - - - - - - - - - 加上 - - - - - - - - - - - - -----------------

解决方案

您需要以传统方式调用它?



这是一个带空格(和标点符号!)的函数名称。

 >>> def _func():
printYOOOO


>>> globals()['Da func name !! 1'] = _func

>>> globals()['Da func name !! 1']()
YOOOO


I am writing a python scripts to call a function.

Normally function are called:

def myCall():
    print "Hello World"

But I would like to name/use the function as:

def my Call():
    print "I did it!"

I knew the world will start thinking why the programmer name the function this ways. Just replace the "space" with "under score" or something else! Hm... but that's not how I want the script to work.

Any suggestion to call the function with "space"?

-------------------------------------------ADD-ON----------------------------------------

Ok guys! I am going to explain how my script works. Specifically why I use space in a function name. There are a lot of curosity in this page, thus I place this add-on to explain why I did it.

I hope this will help everyone understand why I did this :)

Cheer with Respect!

E.g.

===============
Welcome Menu
===============
1. Option 1
2. Option 2
3. Option 3
4. Option 4

I have a user main menu and the main will constantly update every to check before it display the options above

array = ["Option 1", "Option 2", "Option 3", "Option 4"]

The checking are done because when some variable (declare in front the script) are missing, the specific option confirm would not work.

E.g.

for x in range(a) 
    print "Menu Option

I place my menu in a loop (Meaning print once only), The counting of the number in the menu are separated from the string option are auto increment each when there is 1 more element in the array to print the counting. The Option variable are place in the array.

The user will select what he/she want in the options. Let said the user select "Option 1", it will goes to the array (array = ["Option 1", "Option 2", "Option 3", "Option 4"]) to check which position.

Remember the array are not FIXED!!! It change depend on the variable declare in the front of the script.

All vaildation are done to prevent errors, crash, etc!

Finally, I have write a function (with space)

And in the loop, it will call the function (with space). The function will need to link back to the array to change.

-------------------------------------------ADD-ON----------------------------------------

解决方案

Do you need to call it in the conventional way?

Here's a function name with spaces (and punctuation!)

>>> def _func():
    print "YOOOO"


>>> globals()['Da func name!!1'] = _func

>>> globals()['Da func name!!1']()
YOOOO

这篇关于Python:在函数名称中使用空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆