开发者

Python: Use spaces in a function name?

开发者 https://www.devze.com 2023-04-10 09:13 出处:网络
I am writing a python scripts to call a function. Normally function are called: def myCall(): print \"Hello World\"

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.

Reme开发者_JAVA百科mber 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


Short of modifying the Python interpreter, you can't (at least, not with your sample syntax)

Spaces are used to delimit tokens, not just in Python, but in English (and several other languages) too. So, even if you found a way to do this, your code would be much less readable, since you might have to read a whole phrase to figure out what function is being used on any given line.

It would be akin to removingallthespacesinasentence; you'd have to read the whole thing and spend significantly more time parsing it mentally just to know what the code says (let alone what it does!).


In Python 3, you can use identifiers containing symbols but not punctuation. Try a symbol that is almost invisible, such as the middle dot (U+00B7).

# coding: utf-8

def my·Call():
    print("Hello World")

my·Call()


If it's any consolation, the Tcl programming language emphasizes that "Everything is a string", and that strings are sequences of any Unicode characters. In particular, Tcl "proc"-s (the closest canonical correspondent to Python's functions) may be named with strings which embed blanks.


I'm quite late to the party, but here it goes. I would normally try to use something like a dict for this. But if you really want a function you could try:

import test  # Name of the module

def hello():
    print('Hello World')

setattr(test, 'Hello World', hello)

# This can be in the calling module
getattr(test, 'Hello World')()
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号