Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

For good style, should I define inner classes before methods or vice versa in Python?

I read PEP 8: Style Guide for Python code, but I couldn't find anything on what order I should define inner classes or methods in a class.

For example, should I do

class CustomClass(BaseClass):
    class Meta:
        foo = self.bar

    def foo_bar(self):
        return False

    bar = 1

or

class CustomClass(BaseClass):
    def foo_bar(self):
        return False

    class Meta:
        foo = self.bar

    bar = 1

?

EDIT: Here is a Django source code. They actually define fields, then inner classes, then methods:

class LogEntry(models.Model):
    action_time = models.DateTimeField(_('action time'), auto_now=True)
    user = models.ForeignKey(User)
    content_type = models.ForeignKey(ContentType, blank=True, null=True)
    object_id = models.TextField(_('object id'), blank=True, null=True)
    object_repr = models.CharField(_('object repr'), max_length=200)
    action_flag = models.PositiveSmallIntegerField(_('action flag'))
    change_message = models.TextField(_('change message'), blank=True)

    objects = LogEntryManager()

    class Meta:
        verbose_name = _('log entry')
        verbose_name_plural = _('log entries')
        db_table = 'django_admin_log'
        ordering = ('-action_time',)

    def __repr__(self):
        return smart_unicode(self.action_time)

    def __unicode__(self):
        if self.action_flag == ADDITION:
            return _('Added "%(object)s".') % {'object': self.object_repr}
        elif self.action_flag == CHANGE:
            return _('Changed "%(object)s" - %(changes)s') % {'object': self.object_repr, 'changes': self.change_message}
        elif self.action_flag == DELETION:
            return _('Deleted "%(object)s."') % {'object': self.object_repr}

        return _('LogEntry Object')

    def is_addition(self):
        return self.action_flag == ADDITION

    def is_change(self):
        return self.action_flag == CHANGE

    def is_deletion(self):
        return self.action_flag == DELETION

    def get_edited_object(self):
        "Returns the edited object represented by this log entry"
        return self.content_type.get_object_for_this_type(pk=self.object_id)

    def get_admin_url(self):
        """
        Returns the admin URL to edit the object represented by this log entry.
        This is relative to the Django admin index page.
        """
        if self.content_type and self.object_id:
            return mark_safe(u"%s/%s/%s/" % (self.content_type.app_label, self.content_type.model, quote(self.object_id)))
        return None
like image 629
hobbes3 Avatar asked Apr 13 '12 15:04

hobbes3


People also ask

When Should inner classes be used?

We use inner classes to logically group classes and interfaces in one place to be more readable and maintainable. Additionally, it can access all the members of the outer class, including private data members and methods.

Why inner classes are avoided in Python?

A parent class can have one or more inner classes but generally inner classes are avoided. We can make our code even more object-oriented by using an inner class. A single object of the class can hold multiple sub-objects. We can use multiple sub-objects to give a good structure to our program.

Can you create an inner class inside a method?

Method-local Inner Class In Java, we can write a class within a method and this will be a local type. Like local variables, the scope of the inner class is restricted within the method. A method-local inner class can be instantiated only within the method where the inner class is defined.

Why do we use inner class in Python?

Advantages of inner class: Logical grouping of classes: If a class is useful to only one other class then it is logical to embed it in that class and keep the two together. Nesting such "helper classes" makes their package more streamlined.


1 Answers

The generally accepted practice is to define fields, then inner classes, then methods.

like image 57
Ignacio Vazquez-Abrams Avatar answered Nov 14 '22 22:11

Ignacio Vazquez-Abrams