简体   繁体   English

Python类常量使用类init方法吗?

[英]Python class constant using class init method?

I've made a class which can be compared and sorted inside common data structures. 我做了一个可以在通用数据结构中进行比较和排序的类。

The thing is that I wanted to make two class constants for the maximum and minimum values that class can take. 问题是我想为类可以采用的最大值和最小值设置两个类常量。 So I could call this value just importing MyClass and writing 所以我可以只导入MyClass并编写该值

obj = MyClass.MY_MAX_CONSTANT

The thing is that calling the constructor or init method to initialize these constants is not allowed. 事实是不允许调用构造函数或init方法初始化这些常量。

In Java this would be declared as static and it would work, but I don't know how can I do a class / static constant in Python using the constructor / init method. 在Java中,它将被声明为static并且可以正常工作,但是我不知道如何使用构造函数/ init方法在Python中执行类/静态常量。 Haven't found much googling but some general recipes for constants and suggestions for making properties. 还没有找到太多的谷歌搜索功能,但有一些关于常量的通用配方以及关于制作属性的建议。

I don't need a mechanism to avoid changing the constant value since I'm definitely not changing it. 我不需要一种机制来避免更改常量值,因为我绝对不会更改它。

My first try was: 我的第一次尝试是:

class MyClass(object):
    MY_MAX_CONSTANT = MyClass(10,10)
    MY_MIN_CONSTANT = MyClass(0,0)

    def __init__(self, param1, param2): # Not the exact signature, but I think this works as an example 
        # We imagine some initialization work here
        self.x = param1
        self.y = param2


    # SORT FUNCTIONS
    def __cmp__(self, other):
        # Implementation already made here

    def __eq__(self, other):
        # Implementation already made here

    def __ne__(self, other):
        # Implementation already made here

    def __ge__(self, other):
        # Implementation already made here

    # And so on...

A second try, by using some functions for each constant: 第二次尝试,通过对每个常量使用一些函数:

class MyClass(object):
    def __init__(self, param1, param2): # Not the exact signature, but I think this works as an example 
        # We imagine some initialization work here
        self.x = param1
        self.y = param2
        MY_MAX_CONSTANT = None
        MY_MIN_CONSTANT = None

    @staticmethod
    def get_max(self):
        if not MyClass.MY_MAX_CONSTANT:
            MyClass.MY_MAX_CONSTANT = MyClass(10,10)
        return MyClass.MY_MAX_CONSTANT

    @staticmethod
    def get_min(self):
        if not MyClass.MY_MIN_CONSTANT:
            MyClass.MY_MIN_CONSTANT = MyClass(0,0)
        return MyClass.MY_MIN_CONSTANT    

    # SORT FUNCTIONS (I'm not writing them twice for spacing)

But I wanted to avoid strange function mechanisms only for making two constants. 但我想避免奇怪的函数机制仅用于生成两个常量。

I prefer the constant being in the class and not a module because it feels more natural to me, but I'm hearing any advice or suggestion. 我更喜欢在班级而不是在模块中学习常量,因为它对我来说更自然,但是我听到了任何建议。 Can anyone point me a better pythonic solution? 谁能指出我一个更好的pythonic解决方案?

Thanks 谢谢

Add your constants after creating your class, you are allowed to add more class attributes: 创建类添加常量,可以添加更多的类属性:

class MyClass:
    # ...

MyClass.MY_MAX_CONSTANT = MyClass(10, 10)
MyClass.MY_MIN_CONSTANT = MyClass(0, 0)

Only when the class statement has completed running is the class object available, bound to the name MyClass . 只有当class语句完成运行后,绑定到名称MyClass的类对象才可用。 You can't create instances before this point. 在此之前,您无法创建实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM