为了获得好的风格,我应该在Python中先定义内部类,然后再定义方法,还是反之亦然?
我读了,但是我找不到任何关于我应该在类中定义内部类或方法的顺序的东西 例如,我应该怎么做为了获得好的风格,我应该在Python中先定义内部类,然后再定义方法,还是反之亦然?,python,coding-style,inner-classes,Python,Coding Style,Inner Classes,我读了,但是我找不到任何关于我应该在类中定义内部类或方法的顺序的东西 例如,我应该怎么做 class CustomClass(BaseClass): class Meta: foo = self.bar def foo_bar(self): return False bar = 1 或 ? 编辑:这里是Django的源代码。它们实际上定义了字段,然后定义了内部类,然后定义了方法: class LogEntry(models.Model
class CustomClass(BaseClass):
class Meta:
foo = self.bar
def foo_bar(self):
return False
bar = 1
或
?
编辑:这里是Django的源代码。它们实际上定义了字段
,然后定义了内部类
,然后定义了方法
:
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
普遍接受的做法是先定义字段,然后定义内部类,然后定义方法。我想你指的是内部类。子类是完全不同的。@jpm:绝对不同。为了反映这一点,我做了一些编辑。我不记得在Python程序中见过嵌套类。不管Django做什么我都会同意。为什么有人喜欢这个?@保罗:老实说,我不能告诉你。这似乎是每个人都在做的事情。我要指出的是,内部类在Python中没有太多用途。因为python中没有“私有”这类东西,模块可以提供您所需的大部分结构控制。在python中,没有。但是框架(例如Django)将它们用于各种事情。@Lattyware将它们视为“成员类”而不是“内部类”更有用,因为它们完全等同于设置一个包含类的变量(并且它们不能访问外部类)。
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