2

hello I might have a strange question. im working with somemodule in which some functions have similiar names, however they differ in a piece of the function name. to access them more flexible way i was thinking if it would be possible to create a variable or something that would represent a variable. or is there another way to access the different function with a similiar name in a flexible manner? one idea would have to have something like this:

a = print() # a should be the print function now a('hello world.') # this should print: hello world

obviously this doesn't work but is there some way to archieve what im looking for? thank you in advance!

hegal
  • 81
  • 7
  • 1
    Functions are first class citizens. You can assign them to variables if you want, but you would need e.g. `a = print` not `a = print()`. The latter usage assigns a function *call* to `a`, which isn't what you want. – John Coleman Feb 15 '18 at 19:09
  • 2
    Possible duplicate of [Assigning a function to a variable](https://stackoverflow.com/questions/10354163/assigning-a-function-to-a-variable) – John Coleman Feb 15 '18 at 19:12

1 Answers1

1

The common way to do this is to rename the function when you import it. For example:

from somemodule import print as print_this

Now you use print_this() to call that function.

RJH
  • 66
  • 4