Python Forum
Dynamic return ? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Dynamic return ? (/thread-25446.html)



Dynamic return ? - JohnnyCoffee - Mar-30-2020

I receive in a function two parameters (Class and Method) with dynamic values that serve to reference a certain class and access the method on the return of the function. Working with dynamic data I imagined something like this :

v_return = v_class + "." + v_method + "()"
return v_return
The problem is that I need to execute the return to call the class method retrieved in the parameter and not print the string on the screen :

Main.index()



RE: Dynamic return ? - stullis - Mar-30-2020

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.


RE: Dynamic return ? - JohnnyCoffee - Apr-01-2020

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