简体   繁体   English

在 Python 中使用类作为命名空间是个好主意吗

[英]Is it a good idea to using class as a namespace in Python

I am putting a bunch of related stuff into a class.我正在把一堆相关的东西放到一个类中。 The main purpose is to organize them into a namespace.主要目的是将它们组织到一个命名空间中。

class Direction:

  north = 0
  east = 1
  south = 2
  west = 3

  @staticmethod
  def turn_right(d):
    return turn_to_the_right

  @staticmethod
  def turn_left(d):
    return turn_to_the_left



# defined a short alias because direction will be used a lot
D = Direction

d0 = D.north
d1 = D.turn_right(d)

There is not much object concept involved.涉及的对象概念并不多。 In C++, I will be using the actual language keyword namespace .在 C++ 中,我将使用实际语言关键字namespace There is no such thing in Python. Python 中没有这样的东西。 So I am trying to use class for this purpose.所以我试图为此目的使用class

Is this a good idea?这是一个好主意吗? Any pitfall with this approach?这种方法有什么陷阱吗?

I've just answer a related question yesterday.昨天刚回答了一个相关的问题。 This question is asked in a different way.这个问题以不同的方式提出。 It is an actual decision I need to make for myself.这是我需要为自己做出的实际决定。

Static method vs module function in python - Stack Overflow python中的静态方法与模块函数 - 代码日志

Static method vs module function in python python中的静态方法与模块函数

Yes, indeed.确实是的。 You can use Python classes strictly for namespacing as that is one of the special things they can do and do differently than modules.您可以严格地将 Python 类用于命名空间,因为这是它们可以做的特殊事情之一,并且与模块不同。 It's a lot easier to define a class as a namespace inline in a file than to generate more files.将类定义为文件中的内联命名空间比生成更多文件要容易得多。 You should not do it without commenting your code saying what it's for.你不应该在没有注释你的代码的情况下说明它的用途。 Python classes come in a lot of different forms and purposes and this makes difficulty understanding code you have not seen before. Python 类有很多不同的形式和用途,这使得理解您以前从未见过的代码变得困难。

A Python class used as a namespace is no less a Python class than one that meets the perception of what a class is in other languages.用作命名空间的 Python 类不亚于 Python 类,它符合其他语言中对类的理解。 Python does not require a class to be instantiated to be useful. Python 不需要实例化一个类才有用。 It does not require ivars and does not require methods.它不需要ivars,也不需要方法。 It is fairly flexible.它相当灵活。

Clases can contain other classes too.类也可以包含其他类。

Lots of people have their ideas about what is or isn't Pythonic.很多人对什么是或不是 Pythonic 都有自己的想法。 But if they were all worried about something like consistency, they'd push to have things like len() dir() and help() be a method of objects rather than a global function.但是,如果他们都担心一致性之类的事情,他们会推动让len() dir()help()成为对象的方法而不是全局函数。

Do what works, comment / document it if it isn't usual or obvious usage.做一些有效的事情,如果它不常用或不明显的用法,请对其进行评论/记录。

No. Stick it in a module instead.不,而是将其粘贴在模块中。

Python doesn't have namespaces in the same way that C++ does, but modules serve a somewhat similar purpose (that is, grouping "like" classes and functions together, and giving them unique names to avoid clashes). Python 不像 C++ 那样具有命名空间,但模块的用途有些相似(即将“相似”的类和函数组合在一起,并赋予它们唯一的名称以避免冲突)。

Edit编辑
I saw the comment you posted to your question.我看到你对你的问题发表的评论。 To answer more explicitly, no, in Pythonic code it's not really correct to use a class to emulate a namespace.更明确地回答,不,在 Pythonic 代码中,使用类来模拟命名空间并不是真正正确的。 Modules are there to group related classes, functions, and variables -- use a module instead.模块用于对相关的类、函数和变量进行分组——改用模块。 A class represents a "thing" that has a behavior (methods) and data (instance variables) -- it's not just a collection of standalone functions and variables.类代表具有行为(方法)和数据(实例变量)的“事物”——它不仅仅是独立函数和变量的集合。

It depends on the situation;这取决于实际情况; if you can stick a constant in the module and have it make sense, by all means do so, but putting them in the class can make their meaning more obvious, and allow similar constants to have more "abstraction": placing them in the ServerError class makes more sense than having them all prepended with SERVER_ERROR residing freely in the module.如果你可以在模块中粘贴一个常量并让它有意义,那么一定要这样做,但是将它们放在类中可以使它们的含义更加明显,并允许类似的常量具有更多的“抽象”:将它们放在ServerError类比让它们都在模块中自由驻留SERVER_ERROR更有意义。

Do what is most intuitive, but try to avoid namespace pollution.做最直观的事情,但尽量避免命名空间污染。

Yes, it's fine.是的,没关系。 You can even use property to make methods look like attributes.您甚至可以使用property使方法看起来像属性。

If you have a big class, it might be neater to use a module如果你有一个大班级,使用模块可能会更整洁

In my opinion, a class is a class , and a Namespace is a namespace .在我看来,一个class就是一个,一个Namespace就是一个命名空间 You can use argparse.Namespace like so to create a namespace:您可以像这样使用argparse.Namespace来创建命名空间:

from argparse import Namespace

directions = Namespace(
    north = 0,
    east  = 1,
    south = 2,
    west  = 3,
)

print(directions.north)  # 0
print(directions.east)   # 1
print(directions.south)  # 2
print(directions.west)   # 3

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

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