What is the correct code for Python public static data members?

Hi
In all my Java games, I have used code like :
public static int STAR_TYPE_RED_DWARF=1
public static int STAR_TYPE_WHITE_DWARF=2
public static int STAR_TYPE_BLUE_GIANT=3
public static int STAR_TYPE_SUPER_GIANT=4
and many more game object constants

What is the best way to replicate this in python, so these constants are automatically associated with its object when writing code?
I haven't seen anything like this yet

Thanks
beavoidAsked:
Who is Participating?
 
peprConnect With a Mentor Commented:
Whether to put it into a class or to a separate module, it is a design decision. If the value makes sense only in the context of the class, then it should be part of the class. If the STAR_TYPE_SUPER_GIANT makes sense also outside that class, then it should be separated.

It is probably not a good idea to make one giant constants.py. You have to think whether the things make sense together or not. The reason is that you may want to use the part of the project for another project. And you want to reuse the file. It may never happen. Anyway, if it happens and you just copy some sources and touch almost nothing, then the things were designed correctly.

On the other hand, the source code is not carved to granite. It can be changed later.
0
 
aikimarkConnect With a Mentor Commented:
Try something like this
class MyGameClass:
    STAR_TYPE_RED_DWARF=1
    STAR_TYPE_WHITE_DWARF=2
    STAR_TYPE_BLUE_GIANT=3
    STAR_TYPE_SUPER_GIANT=4

Open in new window

0
 
aikimarkCommented:
You might also use the @property decorator
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
peprConnect With a Mentor Commented:
To add to what aikimark suggests in the first comment... (You only have to be careful not to assign the identifier a value in the object method. It would create a non-class variable that would mask the existence of the class variable.)

Python is a hybrid language. On the other hand, Java is not. This way a module with constants must be pretended using static public variables of a class. The same way, plain function must be simulated as public static methods of a class (in Java). You want to have the values bound to the class. Anyway, it need not to be. You can create a module with initialized variables.

stars.py
STAR_TYPE_RED_DWARF=1
STAR_TYPE_WHITE_DWARF=2
STAR_TYPE_BLUE_GIANT=3
STAR_TYPE_SUPER_GIANT=4

Open in new window


And then you can:
import stars
...

    a = stars.STAR_TYPE_SUPER_GIANT

Open in new window

1
 
beavoidAuthor Commented:
awesome.
Thanks

Are coders still putting these constants in their appropriate classes, or making one giant constants.py?
0
 
beavoidAuthor Commented:
Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.