Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
# global scope these variables so that
# we can use them throughout our code
Car = None
Bus = None
# exciting stuff happening here
# now, based on the variable vehicle
# we want to import a specific module
if vehicle == "car":
# equivalent to: from import Car
_Car = __import__("", globals(), locals(), ["Car"])
Car = _Car.Car
if vehicle == "bus":
# equivalent to: from vehicle.four_wheels.bus import Bus
_Bus = __import__("vehicle.four_wheels.bus", globals(), locals(), ["Bus"])
Bus = _Bus.Bus
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment