3

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

18
  • 8
    Why would the programmer name the function this way? Just replace the space with a under score or something else! - The World Commented Oct 4, 2011 at 17:27
  • 7
    Just write your own language and name it something else than Python... Commented Oct 4, 2011 at 17:28
  • 27
    +1 @JBernado. Maybe Py thon? Commented Oct 4, 2011 at 17:29
  • 3
    Please provide a minimal example so we understand why you'd want to do this. Commented Oct 4, 2011 at 17:33
  • 3
    I respond to what Ezylryb has labeled "ADD-ON": the tactics are mistaken. Do NOT create a collection of functions, each one named by a menu selection; create one function (whose name embeds no blank!), and pass it a pertinent menu-selection string. What I describe is far more Pythonic than the proliferate-functions approach Ezylryb appears to favor. Commented Oct 5, 2011 at 22:25

6 Answers 6

52

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
Sign up to request clarification or add additional context in comments.

7 Comments

Ezylryb... seriously? You'd rather call a function by doing globals()['Da func name!!1'] instead of my_call()? +1 for the answer
I was told specifically not to touch the main code (previous programmer have written) and continue scripting. I am just looking a better way to make my life easy, thus calling functions
@Ezylryb i am still curious what are you trying to achieve with a space?
Hi guys, I have place the add-on section to explain why I need to use function with <space>. Thanks!
saw this 6 years after the posting time, and this does entertain me. one irrelevant upvote is coming.
|
5

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

2 Comments

"When a user uploads a file, a metadata entry is created and stored in mongo"{ //test goes here. } Spaces have value.
Hi guys, I have place the add-on section to explain why I need to use function with <space>. Thanks!
4

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

2 Comments

Hi guys, I have place the add-on section to explain why I need to use function with <space>. Thanks!
Yes, that is almost invisible.
1

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.

1 Comment

Hi guys, I have place the add-on section to explain why I need to use function with <space>. Thanks!
1

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')()

Comments

0

For anyone coming from Scratch (or Smalltalk), wanting to recreate

https://www.ghacks.net/2019/01/05/programming-for-kids-scratch-3-0-launches/

or

| rectangles aPoint collisions |
rectangles := OrderedCollection
  with: (Rectangle left: 0 right: 10 top: 100 bottom: 200)
  with: (Rectangle left: 10 right: 10 top: 110 bottom: 210).
aPoint := Point x: 20 y: 20.
collisions := rectangles select: [:aRect | aRect containsPoint: aPoint].

Python allows to introduce spacing and arguments interspersed with the function name through the use of named (keyword) arguments.

So, the glide from scratch can become something like this in Python

glideSecs(1, to=random_point)

There's no helping to avoid the visual noise from the commas, equal signs, and parens, but much of the spirit of the original can be captured.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.