简体   繁体   English

将方法移动到另一个Python文件

[英]Move methods to another Python file

I want to move my methods (method_2 and method_3) from the main.py file to other python files (such as method_2.py and method_3.py) and call them from there. 我想将我的方法(method_2和method_3)从main.py文件移动到其他python文件(例如method_2.py和method_3.py)并从那里调用它们。

I have an issue that these two functions need uasyncio and uasyncio.asyn modules which is also requiring for method_1 still inside main.py. 我有一个问题,这两个函数需要uasyncio和uasyncio.asyn模块,这也需要method_1仍然在main.py内。 If I add these modules in each file (method_2.py and method_3.py), Will it not cause multiple inheritance when i call them from main.py? 如果我在每个文件中添加这些模块(method_2.py和method_3.py),当我从main.py中调用它们时,它是否会导致多重继承? Because main.py has already used these modules (uasyncio and uasyncio.asyn). 因为main.py已经使用了这些模块(uasyncio和uasyncio.asyn)。

main.py; main.py;

import uasyncio as asyncio
import uasyncio.asyn as asyn

loop = asyncio.get_event_loop()

async def handle_client(reader, writer):
    loop.create_task(asyn.Cancellable(method_1)())

    loop.create_task(asyn.Cancellable(method_2)())

    loop.create_task(asyn.Cancellable(method_3)())

@asyn.cancellable
async def method_1():
    print('method_1 is running')

# i want to move this function to another class or py file (for ex: method_2.py) and call it from there
@asyn.cancellable
async def method_2():
    print('method_2 is running')

# i want to move this function to another class or py file (for ex: method_3.py) and call it from there
@asyn.cancellable
async def method_3():
    print('method_3 is running')

loop.create_task(asyncio.start_server(handle_client, ipAddress, 5700))
loop.run_forever()

method_2.py; method_2.py;

import uasyncio as asyncio
import uasyncio.asyn as asyn

@asyn.cancellable
async def method_2():
    print('method_2 is running')

method_3.py; method_3.py;

import uasyncio as asyncio
import uasyncio.asyn as asyn

@asyn.cancellable
async def method_3():
    print('method_3 is running')

Revised_main.py (which i have considered); Revised_main.py(我考虑过);

import uasyncio as asyncio
import uasyncio.asyn as asyn

loop = asyncio.get_event_loop()

async def handle_client(reader, writer):
    loop.create_task(asyn.Cancellable(method_1)())

    import method_2
    loop.create_task(asyn.Cancellable(method_2.method_2)())

    import method_3
    loop.create_task(asyn.Cancellable(method_3.method_3)())

@asyn.cancellable
async def method_1():
    print('method_1 is running')

loop.create_task(asyncio.start_server(handle_client, ipAddress, 5700))
loop.run_forever()

Will it not cause multiple inheritance when i call them from main.py? 当我从main.py调用它时,它不会导致多重继承吗?

It will not, because import is not inheritance. 它不会,因为import不是继承。 Inheritance in Python looks like this: Python中的继承如下所示:

class Child(Parent):
    # ...

What you're doing is fine and normal, you can import modules in as many Python files as you like, so long as the import dependencies are not circular (eg it would be bad if A imports B which imports A ). 您正在做的事情很好而且很正常,您可以在任意数量的Python文件中导入模块,只要导入依赖关系不是循环的(例如,如果A导入B导入A则会很糟糕)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM