Python Forum
Calling functions by making part of their name with variable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calling functions by making part of their name with variable
#1
Hi,

I am new to python and programming in general.

I have the following 4 functions:
  • __scroll_down
  • __scroll_up
  • __scroll_left
  • __scroll_right

I also have a variable called direction which can contain either: down, up, left or right

Is there a way to call a function based on the variable name, maybe something like this __scroll_{direction}() ?

Thank you for your time and help
Crouz
Reply
#2
Use a dictionary
scroll = {
    'down': __scroll_down,
    'left': __scroll_left,
    'right': __scroll_right,
    'up': __scroll_up,
}
scroll['up']()
What is the purpose of the double underscores?
You could also define a function
scroll_dispatch = {
    'down': __scroll_down,
    'left': __scroll_left,
    'right': __scroll_right,
    'up': __scroll_up,
}
def scroll(direction):
    return scroll_dispatch[direction]()

scroll('up')
Reply
#3
Depending on what your __scroll_xx functions is doing, you may want to have only one function named "scroll" and provide te direction as an argument to the function.

And what is the purpose of the double underscores here?

Regards, noisefoor
Reply
#4
Thank you all for your answers.

Double underscores mean these class methods are private and can only be called within that class or its instances
Reply
#5
(Nov-02-2023, 10:59 AM)crouzilles Wrote: Double underscores mean these class methods are private and can only be called within that class or its instances
That's wrong. Python doesn't know anything like private / protected methods. If a method or attribute shouldn't be accessed from the outside it is marked by convention with a single leading underscore. However, this is a _convention_ it's still accessible.
A double leading underscore will make Python do some name mangling when an instance of the class is created. However, the method or attribute is still accessible. The real world use cases for double leading underscore are more on the rare side.

For a deeper explanation with examples you may want to read e.g. this article about the meaning of underscores in Python code.

Regards, noisefloor
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Whys is asterisk and random variable necessary in these functions? rrowhe4d 5 1,528 Aug-05-2022, 07:53 AM
Last Post: Gribouillis
  How to write a part of powershell command as a variable? ilknurg 2 1,132 Jul-26-2022, 11:31 AM
Last Post: ilknurg
  Calling a base class variable from an inherited class CompleteNewb 3 1,698 Jan-20-2022, 04:50 AM
Last Post: CompleteNewb
  Calling functions from within a class: PYQT6 Anon_Brown 4 3,789 Dec-09-2021, 12:40 PM
Last Post: deanhystad
  How to include input as part of variable name Mark17 4 2,526 Oct-01-2021, 06:45 PM
Last Post: Mark17
Question Calling on a Variable using other Variables jacknewport 4 2,007 Jul-23-2021, 04:18 PM
Last Post: jacknewport
  How to use a variable in Python (2.x) to define decimal part? MDRI 4 2,344 May-07-2021, 12:39 AM
Last Post: MDRI
Question trouble with functions "def", calling/defining them Duck_Boom 13 4,405 Oct-21-2020, 03:50 AM
Last Post: Duck_Boom
  Print part of a variable rs74 4 2,602 Jul-27-2020, 04:39 PM
Last Post: rs74
  Function Recognises Variable Without Arguments Or Global Variable Calling. OJGeorge4 1 2,254 Apr-06-2020, 09:14 AM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020