OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How to define nested class constants?

  • Thread starter Thread starter iamogbz
  • Start date Start date
I

iamogbz

Guest
I have these constants defined in constants.py

Code:
PERMISSION_USER_ADD = 'add_user'
PERMISSION_USER_VIEW = 'view_user'
PERMISSION_USER_EDIT = 'edit_user'
PERMISSION_TOOL_ADD = 'add_tool'
PERMISSION_TOOL_VIEW = 'view_tool'
PERMISSION_TOOL_EDIT = 'edit_tool'

But I do not want to have to type these full constant names out every time I need to add another one.

Coming from Java I could do this.

Code:
class Permission {
    class User {
        const ADD = 'add_user';
        const VIEW = 'view_user';
        const EDIT = 'edit_user';
    }
    class Tool {
        const ADD = 'add_tool';
        const VIEW = 'view_tool';
        const EDIT = 'edit_tool';
    }
}

But all python linters flag this as bad coding practice and searching for the python way to do it hasn't been fruitful so far.

So my question is: What's the pythonic way to define nested constants, in a way that supports easy refactoring?
<p>I have these constants defined in <code>constants.py</code></p>

<pre><code>PERMISSION_USER_ADD = 'add_user'
PERMISSION_USER_VIEW = 'view_user'
PERMISSION_USER_EDIT = 'edit_user'
PERMISSION_TOOL_ADD = 'add_tool'
PERMISSION_TOOL_VIEW = 'view_tool'
PERMISSION_TOOL_EDIT = 'edit_tool'
</code></pre>

<p>But I do not want to have to type these full constant names out every time I need to add another one. </p>

<p>Coming from Java I could do this.</p>

<pre><code>class Permission {
class User {
const ADD = 'add_user';
const VIEW = 'view_user';
const EDIT = 'edit_user';
}
class Tool {
const ADD = 'add_tool';
const VIEW = 'view_tool';
const EDIT = 'edit_tool';
}
}
</code></pre>

<p>But all python linters flag this as bad coding practice and searching for the python way to do it hasn't been fruitful so far.</p>

<p>So my question is: What's the pythonic way to define nested constants, in a way that supports easy refactoring?</p>
 
Top