Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dynamic return ?
#3
(Mar-30-2020, 10:28 PM)stullis Wrote: Another possibility would be utilizing dicts to register the objects and have a lookup() method. Something like this:

class Test:
	def a(self):
		return 5

	def b(self):
		return 10

	def lookup(self, meth):
		return {"a": self.a, "b": self.b}.get(meth, self.a)

x = Test()
x.lookup("b")()
In addition to this, you would need another dict to store instances and look them up.
getattr()
Reply


Messages In This Thread
Dynamic return ? - by JohnnyCoffee - Mar-30-2020, 07:47 PM
RE: Dynamic return ? - by stullis - Mar-30-2020, 10:28 PM
RE: Dynamic return ? - by JohnnyCoffee - Apr-01-2020, 05:34 AM

Forum Jump:

User Panel Messages

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