Why doesn't a dictionary work in classes?

אורי uri at speedy.net
Tue Dec 25 07:45:30 EST 2018


Why does this not work:

class User(ValidateUserPasswordMixin, PermissionsMixin, Entity,
AbstractBaseUser):
GENDER_UNKNOWN = 0
GENDER_FEMALE = 1
GENDER_MALE = 2
GENDER_OTHER = 3
GENDER_MAX_VALUE_PLUS_ONE = 4

GENDER_FEMALE_STRING = 'female'
GENDER_MALE_STRING = 'male'
GENDER_OTHER_STRING = 'other'

GENDER_CHOICES = (
(GENDER_FEMALE, _("Female")),
(GENDER_MALE, _("Male")),
(GENDER_OTHER, _("Other")),
)
GENDER_VALID_VALUES = [choice[0] for choice in GENDER_CHOICES]
GENDERS_DICT = {GENDER_FEMALE: GENDER_FEMALE_STRING, GENDER_MALE:
GENDER_MALE_STRING, GENDER_OTHER: GENDER_OTHER_STRING}
ALL_GENDERS = [GENDERS_DICT[gender] for gender in GENDER_VALID_VALUES]

(it throws an exception: `NameError: name 'GENDERS_DICT' is not defined`)

But this works:

class User(ValidateUserPasswordMixin, PermissionsMixin, Entity,
AbstractBaseUser):
GENDER_UNKNOWN = 0
GENDER_FEMALE = 1
GENDER_MALE = 2
GENDER_OTHER = 3
GENDER_MAX_VALUE_PLUS_ONE = 4

GENDER_FEMALE_STRING = 'female'
GENDER_MALE_STRING = 'male'
GENDER_OTHER_STRING = 'other'

GENDER_CHOICES = (
(GENDER_FEMALE, _("Female")),
(GENDER_MALE, _("Male")),
(GENDER_OTHER, _("Other")),
)
GENDER_VALID_VALUES = [choice[0] for choice in GENDER_CHOICES]
GENDERS_DICT = {GENDER_FEMALE: GENDER_FEMALE_STRING, GENDER_MALE:
GENDER_MALE_STRING, GENDER_OTHER: GENDER_OTHER_STRING}

User.ALL_GENDERS = [User.GENDERS_DICT[gender] for gender in
User.GENDER_VALID_VALUES]

Thanks,
אורי (Uri)
uri at speedy.net


More information about the Python-list mailing list