How to import a python module inside lambda?

sarath madala :

For suppose depending upon the variable I want to import some classes, create its object and return it. for example :

if x=='SomeThing':
  import something
  object = something's object
else:
  import nothing
  object = nothing's object
object.function()

I want to do the above using the lambda how can I do this?

chepner :

It is rarely an issue to simply import both modules unconditionally, and select which module to actually use later.

import something
import nothing

(something if x == 'SomeThing' else nothing).object.function()

If you do need to perform conditional imports, import one or the other, but using the same name.

if x == 'SomeThing':
    import something as thingmodule
else:
    import nothing as thingmodule

thingmodule.object.function()

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=3967&siteId=1