更新代码以获得更好的 python 结构
我有一个代码可以计算一些东西,并且有很多硬编码值和口是心非,我只需要帮助以这样的方式安排它,它不会看起来很混乱。
这不是我的实际代码,而是我的代码的基本结构
def my_code():
a = [func1(x),func2(x),func3(x)]
return a
def func1(x):
func1_x = #calculation using x formula
func1_dict = {}
func1["name"] = #some f1hardcoded name
func1["school"] = "some f1hardcoded value"
func1["school_address"] = "some f1hardcoded value"
func1["score_in_written"] = f" func1 student scored {func1_x} percentage "
func1["score_in_perc"] = func1_x
def func2(x):
func2_x = #some calculation using y formula
func2_dict = {}
func2["name"] = #some f2hardcoded name
func2["school"] = "some f2hardcoded value"
func2["school_address"] = "some f2hardcoded value"
func2["score_in_written"] = f" func1 student scored {func2_x} percentage "
func2["score_in_perc"] = func2_x
def func3(x):
func3_x = #some calculation using z formula
func3_dict = {}
func3["name"] = # f3 related some hardcoded name
func3["school"] = " f3 related some hardcoded value"
func3["school_address"] = " f3 related hardcoded value"
func3["score_in_written"] = f" func1 student scored {func3_x} percentage "
func3["score_in_perc"] = func3_x
对于特定函数中的任何 x 值,硬编码值基本上不会更改,只有百分比分数和 Score_in_writing 会更改。
由于我有很多这样的函数(直到 func9),有什么方法可以将其更改为更好的代码结构? 有什么办法可以让这段代码变得整洁一点吗?
I have a code that calculates some stuff and has a lot of hardcoded values and duplicity, I just need help in arranging it in such a way It wouldn't look messy.
This is not my actual code but basic structure of my code
def my_code():
a = [func1(x),func2(x),func3(x)]
return a
def func1(x):
func1_x = #calculation using x formula
func1_dict = {}
func1["name"] = #some f1hardcoded name
func1["school"] = "some f1hardcoded value"
func1["school_address"] = "some f1hardcoded value"
func1["score_in_written"] = f" func1 student scored {func1_x} percentage "
func1["score_in_perc"] = func1_x
def func2(x):
func2_x = #some calculation using y formula
func2_dict = {}
func2["name"] = #some f2hardcoded name
func2["school"] = "some f2hardcoded value"
func2["school_address"] = "some f2hardcoded value"
func2["score_in_written"] = f" func1 student scored {func2_x} percentage "
func2["score_in_perc"] = func2_x
def func3(x):
func3_x = #some calculation using z formula
func3_dict = {}
func3["name"] = # f3 related some hardcoded name
func3["school"] = " f3 related some hardcoded value"
func3["school_address"] = " f3 related hardcoded value"
func3["score_in_written"] = f" func1 student scored {func3_x} percentage "
func3["score_in_perc"] = func3_x
Hardcoded values basically would not be changed for any x value in a particular function, only the score in percentage and score_in_written would change.
As I have many functions like this (till func9), is there any way I can change this into better code structure?
Is there any way I can make this code a little tidy and clean?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用类来构造重复值,如下所示:
现在,当您想再次访问数据时,您可以简单地执行以下操作:
如果您还有任何问题,请告诉我。
You can use a class to structure the repeated values like this:
Now when you want to access the data again you can simply do:
Let me know if you have any questions further.
您可能会看到创建多个类,并为相似操作的每个小节都有一个类。然后创建组合所有这些类的主类。这样它看起来干净且结构化。只需阅读 Python 类以及如何创建对象即可。
What you could be looking at is creating multiple classes with having a class for each subsection of similar operations. Then create the main class that combines all these classes. That way it looks clean and structured. Just read yourself into python classes and how to create objects.