python中的静态方法与模块函数

时间:2021-11-07 09:05:26

So I have a class in a module that has some static methods. A couple of these static methods just do crc checks and stuff, and they're not really useful outside of the class (I would just make them private static methods in java or C++). I'm wondering if I should instead make them global class functions (outside of the class).

我在一个模块中有一个类,它有一些静态方法。这些静态方法中有几个只执行crc检查之类的操作,它们在类之外并不是很有用(我只会在java或c++中使它们成为私有静态方法)。我想知道我是否应该将它们设置为全局类函数(在类之外)。

Is there any benefit for doing it either way? The class is being imported by from module import class so I'm not worried about having those modules pulled in as well. But should I just make them class methods so that from module import * is safer or something?

这样做有什么好处吗?这个类是由模块导入类导入的,所以我不担心这些模块也会被导入。但是我是否应该让它们成为类方法,以便从模块导入*更安全些?

3 个解决方案

#1


7  

Prefixing the function names with a single underscore is a convention to say that they are private, and it will also prevent them from being imported with a from module import *.

在函数名前面加上一个下划线是表示它们是私有的约定,它还将防止用一个from模块导入*来导入它们。

Another technique is to specify an __all__ list in the module - this can just be done in the module itself (you don't need an __init__.py file)

另一种技术是在模块中指定__all__列表——这可以在模块本身中完成(不需要__init__)。py文件)

__all__ = ['my_class_name']

This is more of a whitelist approach, so you can have full control over what gets imported without using leading underscores.

这更多的是一种白名单方法,所以您可以完全控制导入的内容,而无需使用前面的下划线。

So unless your methods logically belong in the class, and from your description they don't, I would leave them as module level functions and use one of these two approaches to make them private.

因此,除非你的方法在逻辑上属于类,而且根据你的描述,它们不属于类,否则我将它们作为模块级函数使用这两种方法中的一种使它们成为私有的。

#2


3  

Make them module-level functions, and prefix them with a single underscore so that consumers understand that they are for private use.

让它们成为模块级的函数,并在它们前面加上一个下划线,这样消费者就能理解它们是供私人使用的。

#3


3  

If they are not useful outside of the class, what is the motivation to make them module methods? Keeping them as static method makes the name space cleaner.

如果它们在类之外没有用处,那么使它们成为模块方法的动机是什么?保持它们为静态方法可以使名称空间更简洁。

The only advantage to move it outside maybe so that people can reference them without using qualified them the class name. Say you have a log method that got reference in a ton of places, this may make sense as a stylistic choice.

把它移到外面的唯一好处是人们可以在不使用限定的类名的情况下引用它们。假设您有一个日志方法,它在很多地方都有引用,这可能作为一种风格选择是有意义的。

#1


7  

Prefixing the function names with a single underscore is a convention to say that they are private, and it will also prevent them from being imported with a from module import *.

在函数名前面加上一个下划线是表示它们是私有的约定,它还将防止用一个from模块导入*来导入它们。

Another technique is to specify an __all__ list in the module - this can just be done in the module itself (you don't need an __init__.py file)

另一种技术是在模块中指定__all__列表——这可以在模块本身中完成(不需要__init__)。py文件)

__all__ = ['my_class_name']

This is more of a whitelist approach, so you can have full control over what gets imported without using leading underscores.

这更多的是一种白名单方法,所以您可以完全控制导入的内容,而无需使用前面的下划线。

So unless your methods logically belong in the class, and from your description they don't, I would leave them as module level functions and use one of these two approaches to make them private.

因此,除非你的方法在逻辑上属于类,而且根据你的描述,它们不属于类,否则我将它们作为模块级函数使用这两种方法中的一种使它们成为私有的。

#2


3  

Make them module-level functions, and prefix them with a single underscore so that consumers understand that they are for private use.

让它们成为模块级的函数,并在它们前面加上一个下划线,这样消费者就能理解它们是供私人使用的。

#3


3  

If they are not useful outside of the class, what is the motivation to make them module methods? Keeping them as static method makes the name space cleaner.

如果它们在类之外没有用处,那么使它们成为模块方法的动机是什么?保持它们为静态方法可以使名称空间更简洁。

The only advantage to move it outside maybe so that people can reference them without using qualified them the class name. Say you have a log method that got reference in a ton of places, this may make sense as a stylistic choice.

把它移到外面的唯一好处是人们可以在不使用限定的类名的情况下引用它们。假设您有一个日志方法,它在很多地方都有引用,这可能作为一种风格选择是有意义的。