简体   繁体   中英

Best Pythonic way to store variables inside a class

Here are 4 different ways of storing variables. I'm trying to see which way is most Pythonic way in a sense that it is easy to read.

Maybe, creating an external file and place all the variables in there might be helpful? I don't care much about people being able to access these variables into other files. The variables will only be used for this class.


Defining the variables inside the methods.

class Cat(object):
    def __init__(self, color, breed, age):
        self.color = color
        self.breed = breed
        self.age = age

    def is_old_cat(self):
        OLD_AGE = 15

        if self.age > OLD_AGE:
            return True

        return False

    def is_young_cat(self):
        YOUNG_AGE = 5

        if self.age < YOUNG_AGE:
            return True

        return False

    def is_medium_young(self):
        LOW_MEDIUM_AGE = 8
        HIGH_MEDIUM_AGE = 12

        if LOW_MEDIUM_AGE < self.age < HIGH_MEDIUM_AGE:
            return True

        return False

Inside the class.

class Cat(object):
    OLD_AGE = 15
    YOUNG_AGE = 5
    LOW_MEDIUM_AGE = 8
    HIGH_MEDIUM_AGE = 12

    def __init__(self, color, breed, age):
        self.color = color
        self.breed = breed
        self.age = age

    def is_old_cat(self):
        if self.age > self.AGES['OLD_AGE']:
            return True

        return False

    def is_young_cat(self):
        if self.age < self.AGES['YOUNG_AGE']:
            return True

        return False

    def is_medium_young(self):
        if self.AGES['LOW_MEDIUM_AGE'] < self.age < self.AGES['HIGH_MEDIUM_AGE']:
            return True

        return False

Outside the class.

OLD_AGE = 15
YOUNG_AGE = 5
LOW_MEDIUM_AGE = 8
HIGH_MEDIUM_AGE = 12


class Cat(object):
    def __init__(self, color, breed, age):
        self.color = color
        self.breed = breed
        self.age = age

    def is_old_cat(self):
        if self.age > AGES['OLD_AGE']:
            return True

        return False

    def is_young_cat(self):
        if self.age < AGES['YOUNG_AGE']:
            return True

        return False

    def is_medium_young(self):
        if AGES['LOW_MEDIUM_AGE'] < self.age < AGES['HIGH_MEDIUM_AGE']:
            return True

        return False

Inside another class.

class Ages:
    OLD_AGE = 15
    YOUNG_AGE = 5
    LOW_MEDIUM_AGE = 8
    HIGH_MEDIUM_AGE = 12


class Cat(object):
    def __init__(self, color, breed, age):
        self.color = color
        self.breed = breed
        self.age = age

    def is_old_cat(self):
        if self.age > Ages.OLD_AGE:
            return True

        return False

    def is_young_cat(self):
        if self.age < Ages.YOUNG_AGE:
            return True

        return False

    def is_medium_young(self):
        if Ages.LOW_MEDIUM_AGE < self.age < Ages.HIGH_MEDIUM_AGE:
            return True

        return False

There are two ways to do this that I see the most... Are they the most Pythonic? Not sure, so I will leave up to the true python ninjas and not make any bold statements about that.

If these values are specific to cats, you can get away with this.

class Cat(object):
    OLD_AGE = 15
    YOUNG_AGE = 5
    LOW_MEDIUM_AGE = 8
    HIGH_MEDIUM_AGE = 12

    def __init__(self, color, breed, age):
        self.color = color
        self.breed = breed
        self.age = age

    def is_old_cat(self):
        if self.age > self.OLD_AGE:
            return True

        return False

The other way is to make an enum that you can reuse in places outside of your Cat class. Many will say this is the better method.

However, if you do choose to use an enum it may be confusing if you had a Human class where you would want to be clear that 15 is old for cats, not for humans.

from enum import IntEnum
class Age(IntEnum):
    OLD = 15
    YOUNG = 5
    LOW_MEDIUM = 8
    HIGH_MEDIUM = 12

print(Age.OLD) # 15
print(Age.YOUNG) # 5

If you're using the enums in this file only, have them be in the same file. Else, move them to a different file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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