I need to have an import in __init__()
method (because i need to run that import only when i instance the class).
But i cannot see that import outside __init__()
, is the scope limited to__init__
? how to do?
If you want the result of your import to be visible to other objects of the class, you would need to assign the resulting object from your import to a class or instance variable
For example:
>>> class Foo(object):
... def __init__(self):
... import math
... self.bar = math.pi
... def zoo(self):
... return self.bar
...
>>> a = Foo()
>>> a.zoo()
3.141592653589793
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With