user1814016
user1814016

Reputation: 2383

How can I use variables in statements with Python?

sname = "example"
some_class.<sname>.do_stuff()

sname is the name of a subclass in some_class. Is it possible to dynamically reference the string in my call, so some_class.example.do_stuff() is called?

Upvotes: 2

Views: 66

Answers (1)

FatalError
FatalError

Reputation: 54631

You're looking for the getattr() builtin:

getattr(some_class, sname).do_stuff()

Upvotes: 6

Related Questions