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
LVL 1
beavoidAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

aikimarkCommented:
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
peprCommented:
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
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

beavoidAuthor Commented:
awesome.
Thanks

Are coders still putting these constants in their appropriate classes, or making one giant constants.py?
0
peprCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
beavoidAuthor Commented:
Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Game Programming

From novice to tech pro — start learning today.