Why doesn't a dictionary work in classes?

Avi Gross avigross at verizon.net
Tue Dec 25 11:59:49 EST 2018


אורי,

Your indentation did not make it through. So the first thing to guess is what you are asking python to do.

It is often good to not just supply the code but point out what you are trying to do as well as explain the small difference you made in your code that made it work so we can zoom in on that.

It looks like you want to create a class but then you do nothing that follows up. If we indent further lines, it looks like you are creating constant values. The class is not initialized, there are no methods, etc.

Your class definition though brings in parent classes that presumably supply what you need so you are merely extending it by supplying a form of sorts, right?

	" class User(ValidateUserPasswordMixin, PermissionsMixin, Entity, AbstractBaseUser):"

I have no idea where those parent classes are defined and since they are the same in both the failing case and the working case, that should not matter. But if I have no access to that, I cannot easily replicate your code. I can try running some lines at the global level and I note some errors there:

What are the naked underscores doing in this:

GENDER_CHOICES = (
    (GENDER_FEMALE, _("Female")),
    (GENDER_MALE, _("Male")),
    (GENDER_OTHER, _("Other")),
    )

You seem to be making an odd tuple of tuples. Is the first tuple supposed to join the numerical representation of 1, with the character representation of "Female" to make (1,"Female") and the other two tuples something similar? You already declared similar (but all lowercase) strings above. So what is this odd notation for:
	_("Female")

If that section fails, then GENDER_CHOICES does not get created. Did you not get an error message for that?

But moving on to  your error message about not finding the dictionary in what shows up here as one merged line:

" 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]"

I see it as being better formatted as something like this:

    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]

The first line wants to make a list and it looks like you want a list of the first character in Male, Female and Other.

I assume you want to create a dict on the second line(s) looking like this: {1: 'female', 2: 'male', 3: 'other'}

I will not debate with you the choice of 3=2+1 as there are multiple variations on what you call other out there.

The last line, is trying to make a list of values from the dictionary just created. The error message you show suggests the dictionary creation failed on the lines above it.

So what is different in the version that worked?

    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]

What you might have pointed out is that the last line was changed. It gives an expanded name to the variables you used. It is not clear due to the indentation problem if you ran this inside the class definition or outside.

Your variable names are all upper case and do not seem likely to cause confusion with the other parent classes but what about AbstractBaseUser? Is it possible that you may be trying to access a name defined in both contexts and need to carefully spell out which one you want here?

Sorry for the length of my message. I needed to work my way through piece by piece. As always, I might have done it differently so half the task is trying to think more like the writer.

אבי


-----Original Message-----
From: Python-list <python-list-bounces+avigross=verizon.net at python.org> On Behalf Of ????
Sent: Tuesday, December 25, 2018 7:46 AM
To: python-list at python.org
Subject: Why doesn't a dictionary work in classes?

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
--
https://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list