Interesting, you are right. It's still a little strange to me to be accessing class attributes via the self object, but I suppose it is more flexible than accessing them as User.MALE etc. which would make renaming the class awkward.
Best, Alex Ogier On Apr 5, 2012 2:16 PM, "Łukasz Rekucki" <lreku...@gmail.com> wrote: > On 5 April 2012 19:45, Alex Ogier <alex.og...@gmail.com> wrote: > > > >> >> class User(models.Model): > >> >> MALE = 0 > >> >> FEMALE = 1 > >> >> GENDERS = [(MALE, 'Male'), (FEMALE, 'Female')] > >> >> gender = models.IntegerField(choices=GENDERS) > >> >> > >> >> def greet(self): > >> >> return {MALE: 'Hi, boy', FEMALE: 'Hi, girl.'}[self.gender] > >> >> > >> > >> I' sure you meant: > >> > >> def greet(self): > >> return {self.MALE: 'Hi, boy', self.FEMALE: 'Hi, girl.'}[self.gender] > >> > >> Unless you defined MALE/FEMALE as globals too :) Otherwise you'll get > >> a NameError. > >> > >> -- > >> Łukasz Rekucki > > > > As attributes of the class object I'm pretty sure they are in scope. No > > NameErrors there. > > > > That's not how it works. Code that executes when creating a new class > does not define a lexical scope. There is no such thing as "class > scope". Try it yourself: > > http://ideone.com/xbr0q > > -- > Łukasz Rekucki > > -- > You received this message because you are subscribed to the Google Groups > "Django developers" group. > To post to this group, send email to django-developers@googlegroups.com. > To unsubscribe from this group, send email to > django-developers+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/django-developers?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Django developers" group. To post to this group, send email to django-developers@googlegroups.com. To unsubscribe from this group, send email to django-developers+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-developers?hl=en.