简体   繁体   English

避免全局变量但也有太多函数参数(Python)

[英]Avoiding global variables but also too many function arguments (Python)

Let's say I have a python module that has a lot of functions that rely on each other, processing each others results. 假设我有一个python模块,它有很多相互依赖的函数,处理彼此的结果。 There's lots of cohesion. 有很多凝聚力。

That means I'll be passing back and forth a lot of arguments. 这意味着我会来回传递很多争论。 Either that, or I'd be using global variables. 要么是,要么我将使用全局变量。

What are best practices to deal with such a situation if? 如果处理这种情况的最佳做法是什么? Things that come to mind would be replacing those parameters with dictionaries. 想到的事情将是用字典替换这些参数。 But I don't necessarily like how that changes the function signature to something less expressive. 但我不一定喜欢将函数签名更改为不那么具有表现力的东西。 Or I can wrap everything into a class. 或者我可以把所有东西都包装成一个类。 But that feels like I'm cheating and using "pseudo"-global variables? 但这感觉就像我在欺骗并使用“伪”全局变量?

I'm asking specifically for how to deal with this in Python but I understand that many of those things would apply to other languages as well. 我正在专门询问如何在Python中处理这个问题,但我知道其中很多东西也适用于其他语言。

I don't have a specific code example right, it's just something that came to mind when I was thinking about this issue. 我没有正确的代码示例,这只是我在思考这个问题时想到的东西。

Examples could be: You have a function that calculates something. 例子可能是:你有一个计算某事的函数。 In the process, a lot of auxiliary stuff is calculated. 在这个过程中,计算了很多辅助材料。 Your processing routines need access to this auxiliary stuff, and you don't want to just re-compute it. 您的处理例程需要访问这些辅助内容,而您不想只重新计算它。

This is a very generic question so it is hard to be specific. 这是一个非常通用的问题,因此很难具体。 What you seem to be describing is a bunch of inter-related functions that share data. 您似乎要描述的是一组共享数据的相互关联的函数。 That pattern is usually implemented as an Object. 该模式通常作为Object实现。

Instead of a bunch of functions, create a class with a lot of methods. 而不是一堆函数,创建一个包含很多方法的类。 For the common data, use attributes. 对于公共数据,请使用属性。 Set the attributes, then call the methods. 设置属性,然后调用方法。 The methods can refer to the attributes without them being explicitly passed as parameters. 这些方法可以引用属性,而不将它们作为参数显式传递。

As RobertB said, an object seems the clearest way. 正如RobertB所说,一个物体似乎是最清晰的方式。 Could be as simple as: 可以这么简单:

class myInfo:
    def __init__(self, x=0.0, y=0.0):
        self.x = x
        self.y = y
        self.dist = self.messWithDist()

    def messWithDist(self):
        self.dist = math.sqrt(self.x*self.x + self.y*self.y)

blob = myInfo(3,4)
blob.messWithDist()
print(blob.dist)
blob.x = 5
blob.y = 7
blob.messWithDist()
print(blob.dist)

If some of the functions shouldn't really be part of such an object, you can just define them as (non-member, independent) functions, and pass the blob as one parameter. 如果某些函数不应该真正属于这样的对象,您可以将它们定义为(非成员,独立)函数,并将blob作为一个参数传递。 For example, by un-indenting the def of messWithDist, then calling as messWithDist(blob) instead of blob.messWithDist(). 例如,通过取消缩进messWithDist的def,然后调用messWithDist(blob)而不是blob.messWithDist()。

-s -s

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

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