Python Forum
simplifying a stack of elifs
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
simplifying a stack of elifs
#3
i can't use "self" because that might collide with other threads so i think each thread needs its own class or dictionary to keep variables in.

does CPython do locks between threads to access dictionaries? i haven't seen anything to suggest that it does. since Queues are described as having such locks, i suspect nothing else does unless it says it does, so i think i need to keep things separated between threads.

what about defining these action functions inside the function that needs to call them and have it call like this:
    def _agent():
        ...
        def x_foo():
            ... # code to run when request "foo" arrives
        def x_bar():
            ... # code to run when request "bar" arrives

        msg = q.get()
        req = 'x_'+msg[0]
        if req in locals():
            locals()[req]() # call the function to handle this request
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Messages In This Thread
simplifying a stack of elifs - by Skaperen - Aug-14-2019, 04:23 AM
RE: simplifying a stack of elifs - by Gribouillis - Aug-14-2019, 07:05 AM
RE: simplifying a stack of elifs - by Skaperen - Aug-14-2019, 06:13 PM
RE: simplifying a stack of elifs - by Gribouillis - Aug-15-2019, 06:15 AM
RE: simplifying a stack of elifs - by Skaperen - Aug-15-2019, 08:22 PM
RE: simplifying a stack of elifs - by Gribouillis - Aug-15-2019, 09:16 PM
RE: simplifying a stack of elifs - by Skaperen - Aug-15-2019, 11:51 PM
RE: simplifying a stack of elifs - by Gribouillis - Aug-16-2019, 06:51 AM
RE: simplifying a stack of elifs - by Skaperen - Aug-17-2019, 04:13 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Help for simplifying code mmk1995 8 4,259 Sep-24-2019, 02:04 PM
Last Post: perfringo
  a stack of elifs and a try/except Skaperen 2 2,016 Sep-12-2019, 11:39 PM
Last Post: Skaperen
  Simplifying my code ilondire05 5 3,824 Jul-21-2019, 03:21 AM
Last Post: scidam
  My program subtracts fractions, but for some reason isn't simplifying them RedSkeleton007 9 5,978 Mar-03-2018, 11:45 AM
Last Post: Gribouillis
  Simplifying multiple "or" conditions in if statement. rhubarbpieguy 8 102,290 Jul-22-2017, 12:19 PM
Last Post: rhubarbpieguy

Forum Jump:

User Panel Messages

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