我是一名初学者 - 中级自学Python开发者,如何将此过程代码设计为基于类(面向对象)?
在我完成的大多数项目中,我可以看到以下过程重复。我没有任何外部家庭代码的经验,我认为下面的代码不是很专业,因为它不是可重用的,似乎它不适合所有在一个容器中,但在不同的模块上松散耦合的功能。
def get_query():
# returns the query string
pass
def make_request(query):
# makes and returns the request with query
pass
def make_api_call(request):
# calls the api and returns response
pass
def process_response(response):
# process the response and returns the details
pass
def populate_database(details):
# populates the database with the details and returns the status of population
pass
def log_status(status):
# logs the status so that developer knows whats happening
pass
query = get_query()
request = make_request(query)
response = make_api_call(request)
details = process_response(response)
status = populate_database(details)
log_status(status)
如何将此过程设计为基于类的设计?
*“它不可重复使用”* - 您为什么这么认为?一些简短的单一用途函数肯定比将所有东西合并成一个函数更加可重用。 *“不同模块上的松散耦合功能”* - 它们似乎在同一个模块中,松耦合通常被认为是一件好事! – jonrsharpe
@jonrsharpe对于遍布某些模块的函数,我总是发现很难追踪模块,复制代码并使其在第二个项目的模块结构上的另一个项目上工作 – Marty
相关的,可重用的功能应该被打包成一个单一模块,但除此之外,还不清楚为什么你认为现在有什么问题。如果你直接将它传递给类,你最终会得到[*“两种方法,其中之一是'__init__'”*](https://www.youtube.com/watch?v=o9pEzgHorH0)。 – jonrsharpe