Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Use spaces in a function name?

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----------------------------------------

like image 223
Ezylryb Avatar asked Oct 04 '11 17:10

Ezylryb


People also ask

Can function name have space in Python?

There should be no space between the name of a function and the ( (left parenthesis) of its parameter list, unless case of an anonymous literal function.

Can a function name contain spaces?

No, in javascript a function cannot contain spaces.

How do you space a function in Python?

We add space in string in python by using rjust(), ljust(), center() method. To add space between variables in python we can use print() and list the variables separate them by using a comma or by using the format() function.

How do you reference a space in Python?

Python isspace() method is used to check space in the string. It returna true if there are only whitespace characters in the string. Otherwise it returns false.


3 Answers

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
like image 179
JBernardo Avatar answered Oct 19 '22 05:10

JBernardo


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!).

like image 24
Cameron Avatar answered Oct 19 '22 06:10

Cameron


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()
like image 4
Tugrul Ates Avatar answered Oct 19 '22 05:10

Tugrul Ates