Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. There is no such thing in Python. So I am trying to use class for this purpose.

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

Static method vs module function in python

like image 713
Wai Yip Tung Avatar asked Aug 26 '10 15:08

Wai Yip Tung


People also ask

Can I use class as namespace?

No, namespaces and classes are different. However, namespaces and classes both introduce a scope which may be referred to using the scope resolution operator :: . The using namespace N; declaration can only apply to namespaces. It's not possible to do something similar for a class.

Why should I use namespace in Python?

Namespaces help us uniquely identify all the names inside a program. However, this doesn't imply that we can use a variable name anywhere we want. A name also has a scope that defines the parts of the program where you could use that name without using any prefix.

What is namespace of a class in Python?

Namespaces in Python. A namespace is a collection of currently defined symbolic names along with information about the object that each name references. You can think of a namespace as a dictionary in which the keys are the object names and the values are the objects themselves.

Which features in Python helps us to avoid namespace?

prefix: This has the benefit of avoiding long import statements and the prefix helps avoid namespace collisions.


4 Answers

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. 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.

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 does not require a class to be instantiated to be useful. It does not require ivars and does not require methods. It is fairly flexible.

Clases can contain other classes too.

Lots of people have their ideas about what is or isn't 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.

Do what works, comment / document it if it isn't usual or obvious usage.

like image 125
uchuugaka Avatar answered Oct 03 '22 08:10

uchuugaka


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).

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. 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.

like image 23
mipadi Avatar answered Oct 03 '22 09:10

mipadi


Yes, it's fine. You can even use property to make methods look like attributes.

If you have a big class, it might be neater to use a module

like image 4
Katriel Avatar answered Oct 03 '22 09:10

Katriel


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.

Do what is most intuitive, but try to avoid namespace pollution.

like image 4
Humphrey Bogart Avatar answered Oct 03 '22 09:10

Humphrey Bogart