question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using classmethods for constants

See original GitHub issue

I don’t know if it would be a good idea, or if this has been already brought up (I think @leotrs mentioned a similar idea in #310 ).

The idea would be to change the way all the constants are implemented into something that would be similar to namespaces. My idea would be, for example, for the colors, to do something like :

class Color : 
    ...
    @classmethod
    def red(self):
        return <color red>

Thus, one could access the red colour by simply doing Color.red() instead of the ugly RED

Similar reasoning could be applied for every item within constants.py. So we would have, for example, Message.not_setting_font(). We could even go further, by implementing all constants utils within these classes. (for example, utils.interpolate_color)

A major advantage would be that everything would be much better organized and more, I think, clean. The constants would be well arranged by classes (and so we could get auto-completion ;D), On the other hand, it would be a little bit more long to use, as you would have to access to Color every time.

NOTE : I took inspiration on how colors are implemented on the discord python API, but I can"'t find a link to the file 😕

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (10 by maintainers)

github_iconTop GitHub Comments

2reactions
PgBielcommented, Aug 22, 2020

Yes, that’s also what I proposed doing (enum for colors) However, can we please switch to colors with ints? Strings require parsing. Let’s delegate that job to Python

so your example would be

from enum import Enum

class Colors(Enum):
    white = 0x000000
    black = 0xFFFFFF
    ...

## use Colors.<color>.value internally
print(Colors.white.value) ## 0x000000
1reaction
leotrscommented, Oct 9, 2020

Yes it should! I was just making sure I understood correctly. Also, can you please open a new issue for that? This one has ran its course.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Call classmethod in class constant - python - Stack Overflow
To define the constant I wrote a classmethod who return the length in bytes but i can´t call the classfunction in the class...
Read more >
Python Class Method Explained With Examples - PYnative
Create class method using the @classmethod decorator and classmethod() function in Python. Dynamically add or delete class method.
Read more >
classmethod() in Python - GeeksforGeeks
classmethod () methods are bound to a class rather than an object. Class methods can be called by both class and object. These...
Read more >
Class Methods and Class Variables - CodeHS
A class variable is a variable or attribute of a class that is common to all instances of a class. A class method...
Read more >
Constants vs. class methods - rubocop/ruby-style-guide - GitHub
Greetings, I would like to hear some community standpoint regarding this matter. class User < ActiveRecord::Base ROLES = %w(admin moderator ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found