Bằng cách sử dụng phương thức __Import __ (): >> set_mode("Foo")
>> engine_logger:INFO - This is FOO!
1 là một phương thức khó khăn (các phương thức bắt đầu lớp và kết thúc với dấu gạch dưới kép cũng được gọi là phương thức ma thuật) và tất cả các lớp sở hữu nó. Nó được sử dụng để nhập một mô -đun hoặc một lớp trong trường hợp của một lớp. Có một ví dụ về phương pháp này được đưa ra như sau, trong đó chúng tôi sẽ nhập một mô -đun một cách linh hoạt. Tệp mô -đun hiện được sửa đổi là: >> set_mode("Foo")
>> engine_logger:INFO - This is FOO!
1 is a dunder method (methods of class starting and ending with double underscore also called magic method) and all classes own it. It is used to import a module or a class within the instance of a class. There is an example on this method given as follows, in which we will be importing a module dynamically. The module file is now modified as:module.py
>> set_mode("Foo")
>> engine_logger:INFO - This is FOO!
2 >> set_mode("Foo")
>> engine_logger:INFO - This is FOO!
3from modes.modes import Mode
class Foo(Mode):
def __init__(self, *arg, **kwargs):
super(Foo, self).__init__(*arg, **kwargs)
def run(self):
self.LOG.info(f"This is FOO!")
9from modes.modes import Mode
class Foo(Mode):
def __init__(self, *arg, **kwargs):
super(Foo, self).__init__(*arg, **kwargs)
def run(self):
self.LOG.info(f"This is FOO!")
5 from modes.modes import Mode
class Foo(Mode):
def __init__(self, *arg, **kwargs):
super(Foo, self).__init__(*arg, **kwargs)
def run(self):
self.LOG.info(f"This is FOO!")
6from modes.modes import Mode
class Foo(Mode):
def __init__(self, *arg, **kwargs):
super(Foo, self).__init__(*arg, **kwargs)
def run(self):
self.LOG.info(f"This is FOO!")
7from modes.modes import Mode
class Foo(Mode):
def __init__(self, *arg, **kwargs):
super(Foo, self).__init__(*arg, **kwargs)
def run(self):
self.LOG.info(f"This is FOO!")
8>> set_mode("Foo")
>> engine_logger:INFO - This is FOO!
9def set_mode(mode):
""" """
import importlib
module = importlib.import_module('modes.foo')
getattr(module, mode)().run()
0def set_mode(mode):
""" """
import importlib
module = importlib.import_module('modes.foo')
getattr(module, mode)().run()
1def set_mode(mode):
""" """
import importlib
module = importlib.import_module('modes.foo')
getattr(module, mode)().run()
2 def set_mode(mode):
""" """
import importlib
module = importlib.import_module('modes.foo')
getattr(module, mode)().run()
3 from modes.modes import Mode
class Foo(Mode):
def __init__(self, *arg, **kwargs):
super(Foo, self).__init__(*arg, **kwargs)
def run(self):
self.LOG.info(f"This is FOO!")
7def set_mode(mode):
""" """
import importlib
module = importlib.import_module('modes.foo')
getattr(module, mode)().run()
5Dynamic_import.py
>> set_mode("Foo")
>> engine_logger:INFO - This is FOO!
2 def set_mode(mode):
""" """
import importlib
module = importlib.import_module('modes') # only import the package, not modules explicitely
getattr(module, mode)().run()
7from modes.modes import Mode
class Foo(Mode):
def __init__(self, *arg, **kwargs):
super(Foo, self).__init__(*arg, **kwargs)
def run(self):
self.LOG.info(f"This is FOO!")
9from modes.modes import Mode
class Foo(Mode):
def __init__(self, *arg, **kwargs):
super(Foo, self).__init__(*arg, **kwargs)
def run(self):
self.LOG.info(f"This is FOO!")
5 >> set_mode("Foo")
>> AttributeError: module 'modes' has no attribute 'Foo'
0>> set_mode("Foo")
>> AttributeError: module 'modes' has no attribute 'Foo'
1>> set_mode("Foo")
>> AttributeError: module 'modes' has no attribute 'Foo'
2>> set_mode("Foo")
>> engine_logger:INFO - This is FOO!
9>> set_mode("Foo")
>> AttributeError: module 'modes' has no attribute 'Foo'
4>> set_mode("Foo")
>> AttributeError: module 'modes' has no attribute 'Foo'
5 >> set_mode("Foo")
>> AttributeError: module 'modes' has no attribute 'Foo'
6>> set_mode("Foo")
>> AttributeError: module 'modes' has no attribute 'Foo'
7>> set_mode("Foo")
>> engine_logger:INFO - This is FOO!
9>> set_mode("Foo")
>> AttributeError: module 'modes' has no attribute 'Foo'
9>> set_mode("Foo")
>> AttributeError: module 'modes' has no attribute 'Foo'
5 from .foo import Foo
from .bar import Bar
1from .foo import Foo
from .bar import Bar
2>> set_mode("Foo")
>> engine_logger:INFO - This is FOO!
9from .foo import Foo
from .bar import Bar
4from .foo import Foo
from .bar import Bar
5def set_mode(mode):
""" """
import importlib
module = importlib.import_module('modes.foo')
getattr(module, mode)().run()
5from .foo import Foo
from .bar import Bar
7>> set_mode("Foo")
>> AttributeError: module 'modes' has no attribute 'Foo'
5 from .foo import Foo
from .bar import Bar
9>> set_mode("Foo")
>> engine_logger:INFO - This is FOO!
>> set_mode("Bar")
>> engine_logger:INFO - This is BAR!
0>> set_mode("Foo")
>> engine_logger:INFO - This is FOO!
>> set_mode("Bar")
>> engine_logger:INFO - This is BAR!
1>> set_mode("Foo")
>> engine_logger:INFO - This is FOO!
>> set_mode("Bar")
>> engine_logger:INFO - This is BAR!
2Đầu ra
Hi! User_1 Welcome to GfG
Đây tải các mô -đun hoặc lớp Modules can be imported dynamically by the imp module in python. The example below is a demonstration on the using the imp module. It provides the >> set_mode("Foo")
>> engine_logger:INFO - This is FOO!
>> set_mode("Bar")
>> engine_logger:INFO - This is BAR!
4 method to find the module and the >> set_mode("Foo")
>> engine_logger:INFO - This is FOO!
>> set_mode("Bar")
>> engine_logger:INFO - This is BAR!
5 method to import it.Dynamic_import.py
Các mô -đun được thêm vào trong mã trên là nhập các mô -đun tĩnh, tức là trong thời gian biên dịch. Trong Python, chúng ta có thể nhập các mô -đun một cách linh hoạt bằng hai cách
Bằng cách sử dụng phương thức __Import __ ():
>> set_mode("Foo")
>> engine_logger:INFO - This is FOO!
1 là một phương thức khó khăn (các phương thức bắt đầu lớp và kết thúc với dấu gạch dưới kép cũng được gọi là phương thức ma thuật) và tất cả các lớp sở hữu nó. Nó được sử dụng để nhập một mô -đun hoặc một lớp trong trường hợp của một lớp. Có một ví dụ về phương pháp này được đưa ra như sau, trong đó chúng tôi sẽ nhập một mô -đun một cách linh hoạt. Tệp mô -đun hiện được sửa đổi là:>> set_mode("Foo")
>> engine_logger:INFO - This is FOO!
2 >> set_mode("Foo")
>> engine_logger:INFO - This is FOO!
3from modes.modes import Mode
class Foo(Mode):
def __init__(self, *arg, **kwargs):
super(Foo, self).__init__(*arg, **kwargs)
def run(self):
self.LOG.info(f"This is FOO!")
9Hi! User_1 Welcome to GfG
3Hi! User_1 Welcome to GfG
4from modes.modes import Mode
class Foo(Mode):
def __init__(self, *arg, **kwargs):
super(Foo, self).__init__(*arg, **kwargs)
def run(self):
self.LOG.info(f"This is FOO!")
9from modes.modes import Mode
class Foo(Mode):
def __init__(self, *arg, **kwargs):
super(Foo, self).__init__(*arg, **kwargs)
def run(self):
self.LOG.info(f"This is FOO!")
5 from modes.modes import Mode
class Foo(Mode):
def __init__(self, *arg, **kwargs):
super(Foo, self).__init__(*arg, **kwargs)
def run(self):
self.LOG.info(f"This is FOO!")
6from modes.modes import Mode
class Foo(Mode):
def __init__(self, *arg, **kwargs):
super(Foo, self).__init__(*arg, **kwargs)
def run(self):
self.LOG.info(f"This is FOO!")
7from modes.modes import Mode
class Foo(Mode):
def __init__(self, *arg, **kwargs):
super(Foo, self).__init__(*arg, **kwargs)
def run(self):
self.LOG.info(f"This is FOO!")
8>> set_mode("Foo")
>> engine_logger:INFO - This is FOO!
9def set_mode(mode):
""" """
import importlib
module = importlib.import_module('modes.foo')
getattr(module, mode)().run()
0def set_mode(mode):
""" """
import importlib
module = importlib.import_module('modes.foo')
getattr(module, mode)().run()
1def set_mode(mode):
""" """
import importlib
module = importlib.import_module('modes.foo')
getattr(module, mode)().run()
2 def set_mode(mode):
""" """
import importlib
module = importlib.import_module('modes.foo')
getattr(module, mode)().run()
3 from modes.modes import Mode
class Foo(Mode):
def __init__(self, *arg, **kwargs):
super(Foo, self).__init__(*arg, **kwargs)
def run(self):
self.LOG.info(f"This is FOO!")
7def set_mode(mode):
""" """
import importlib
module = importlib.import_module('modes.foo')
getattr(module, mode)().run()
5>> set_mode("Foo")
>> engine_logger:INFO - This is FOO!
2 def set_mode(mode):
""" """
import importlib
module = importlib.import_module('modes') # only import the package, not modules explicitely
getattr(module, mode)().run()
7from modes.modes import Mode
class Foo(Mode):
def __init__(self, *arg, **kwargs):
super(Foo, self).__init__(*arg, **kwargs)
def run(self):
self.LOG.info(f"This is FOO!")
9Hi! User_1 Welcome to GfG
3Hi! User_1 Welcome to GfG
4from modes.modes import Mode
class Foo(Mode):
def __init__(self, *arg, **kwargs):
super(Foo, self).__init__(*arg, **kwargs)
def run(self):
self.LOG.info(f"This is FOO!")
9from modes.modes import Mode
class Foo(Mode):
def __init__(self, *arg, **kwargs):
super(Foo, self).__init__(*arg, **kwargs)
def run(self):
self.LOG.info(f"This is FOO!")
5 >> set_mode("Foo")
>> AttributeError: module 'modes' has no attribute 'Foo'
0>> set_mode("Foo")
>> AttributeError: module 'modes' has no attribute 'Foo'
1>> set_mode("Foo")
>> AttributeError: module 'modes' has no attribute 'Foo'
2from modes.modes import Mode
class Foo(Mode):
def __init__(self, *arg, **kwargs):
super(Foo, self).__init__(*arg, **kwargs)
def run(self):
self.LOG.info(f"This is FOO!")
05from modes.modes import Mode
class Foo(Mode):
def __init__(self, *arg, **kwargs):
super(Foo, self).__init__(*arg, **kwargs)
def run(self):
self.LOG.info(f"This is FOO!")
06>> set_mode("Foo")
>> engine_logger:INFO - This is FOO!
9>> set_mode("Foo")
>> AttributeError: module 'modes' has no attribute 'Foo'
4>> set_mode("Foo")
>> AttributeError: module 'modes' has no attribute 'Foo'
5 >> set_mode("Foo")
>> AttributeError: module 'modes' has no attribute 'Foo'
6>> set_mode("Foo")
>> AttributeError: module 'modes' has no attribute 'Foo'
7>> set_mode("Foo")
>> engine_logger:INFO - This is FOO!
9def set_mode(mode):
""" """
import importlib
module = importlib.import_module('modes.foo')
getattr(module, mode)().run()
0from modes.modes import Mode
class Foo(Mode):
def __init__(self, *arg, **kwargs):
super(Foo, self).__init__(*arg, **kwargs)
def run(self):
self.LOG.info(f"This is FOO!")
12from modes.modes import Mode
class Foo(Mode):
def __init__(self, *arg, **kwargs):
super(Foo, self).__init__(*arg, **kwargs)
def run(self):
self.LOG.info(f"This is FOO!")
9Hi! User_1 Welcome to GfG
3Hi! User_1 Welcome to GfG
4>> set_mode("Foo")
>> engine_logger:INFO - This is FOO!
9>> set_mode("Foo")
>> AttributeError: module 'modes' has no attribute 'Foo'
9>> set_mode("Foo")
>> AttributeError: module 'modes' has no attribute 'Foo'
5 from .foo import Foo
from .bar import Bar
1from .foo import Foo
from .bar import Bar
2from modes.modes import Mode
class Foo(Mode):
def __init__(self, *arg, **kwargs):
super(Foo, self).__init__(*arg, **kwargs)
def run(self):
self.LOG.info(f"This is FOO!")
23from modes.modes import Mode
class Foo(Mode):
def __init__(self, *arg, **kwargs):
super(Foo, self).__init__(*arg, **kwargs)
def run(self):
self.LOG.info(f"This is FOO!")
24from modes.modes import Mode
class Foo(Mode):
def __init__(self, *arg, **kwargs):
super(Foo, self).__init__(*arg, **kwargs)
def run(self):
self.LOG.info(f"This is FOO!")
25from modes.modes import Mode
class Foo(Mode):
def __init__(self, *arg, **kwargs):
super(Foo, self).__init__(*arg, **kwargs)
def run(self):
self.LOG.info(f"This is FOO!")
26>> set_mode("Foo")
>> engine_logger:INFO - This is FOO!
9>> set_mode("Foo")
>> AttributeError: module 'modes' has no attribute 'Foo'
4>> set_mode("Foo")
>> AttributeError: module 'modes' has no attribute 'Foo'
5 >> set_mode("Foo")
>> AttributeError: module 'modes' has no attribute 'Foo'
6>> set_mode("Foo")
>> AttributeError: module 'modes' has no attribute 'Foo'
7>> set_mode("Foo")
>> engine_logger:INFO - This is FOO!
9def set_mode(mode):
""" """
import importlib
module = importlib.import_module('modes.foo')
getattr(module, mode)().run()
0from modes.modes import Mode
class Foo(Mode):
def __init__(self, *arg, **kwargs):
super(Foo, self).__init__(*arg, **kwargs)
def run(self):
self.LOG.info(f"This is FOO!")
12>> set_mode("Foo")
>> engine_logger:INFO - This is FOO!
9>> set_mode("Foo")
>> AttributeError: module 'modes' has no attribute 'Foo'
9>> set_mode("Foo")
>> AttributeError: module 'modes' has no attribute 'Foo'
5 from .foo import Foo
from .bar import Bar
1from .foo import Foo
from .bar import Bar
2from .foo import Foo
from .bar import Bar
7>> set_mode("Foo")
>> AttributeError: module 'modes' has no attribute 'Foo'
5 from .foo import Foo
from .bar import Bar
9>> set_mode("Foo")
>> engine_logger:INFO - This is FOO!
>> set_mode("Bar")
>> engine_logger:INFO - This is BAR!
0>> set_mode("Foo")
>> engine_logger:INFO - This is FOO!
>> set_mode("Bar")
>> engine_logger:INFO - This is BAR!
1>> set_mode("Foo")
>> engine_logger:INFO - This is FOO!
>> set_mode("Bar")
>> engine_logger:INFO - This is BAR!
2Sử dụng mô -đun IMP: Các mô -đun có thể được nhập động bằng mô -đun IMP trong Python. Ví dụ dưới đây là một minh chứng về việc sử dụng mô -đun IMP. Nó cung cấp phương thức
>> set_mode("Foo")
>> engine_logger:INFO - This is FOO!
>> set_mode("Bar")
>> engine_logger:INFO - This is BAR!
4 để tìm mô -đun và phương thức >> set_mode("Foo")
>> engine_logger:INFO - This is FOO!
>> set_mode("Bar")
>> engine_logger:INFO - This is BAR!
5 để nhập nó.from modes.modes import Mode
class Foo(Mode):
def __init__(self, *arg, **kwargs):
super(Foo, self).__init__(*arg, **kwargs)
def run(self):
self.LOG.info(f"This is FOO!")
9from modes.modes import Mode
class Foo(Mode):
def __init__(self, *arg, **kwargs):
super(Foo, self).__init__(*arg, **kwargs)
def run(self):
self.LOG.info(f"This is FOO!")
51from modes.modes import Mode
class Foo(Mode):
def __init__(self, *arg, **kwargs):
super(Foo, self).__init__(*arg, **kwargs)
def run(self):
self.LOG.info(f"This is FOO!")
52>> set_mode("Foo")
>> engine_logger:INFO - This is FOO!
>> set_mode("Bar")
>> engine_logger:INFO - This is BAR!
1from modes.modes import Mode
class Foo(Mode):
def __init__(self, *arg, **kwargs):
super(Foo, self).__init__(*arg, **kwargs)
def run(self):
self.LOG.info(f"This is FOO!")
54def set_mode(mode):
""" """
import importlib
module = importlib.import_module('modes.foo')
getattr(module, mode)().run()
5Đầu ra
Hi! User_1 Welcome to GfG