【发布时间】:2013-09-19 23:01:50
【问题描述】:
好的,在下面的代码中,属性名称应该是什么?
class ClassOne(object):
a = 1
class ClassTwo(object):
some_class = ClassOne
def _get_other_class(self):
return self._other_class
def _set_other_class(self, value):
self._other_class = value
other_class = property(_get_other_class, _set_other_class)
class InnerClass(object):
b = 2
“some_class”是“some_class”因为它是一个属性还是“SomeClass”因为它引用了一个类更合适?与“other_class”相同。所以第二个选项更像是:
class ClassOne(object):
a = 1
class ClassTwo(object):
SomeClass = ClassOne
def _get_other_class(self):
return self._other_class
def _set_other_class(self, value):
self._other_class = value
OtherClass = property(_get_other_class, _set_other_class)
class InnerClass(object):
b = 2
如果大写它们似乎更清楚它们是类,但是 pep8 说属性应该是带有下划线的小写。然而,InnerClass 最终成为 ClassTwo 的一个属性,名称大写。
【问题讨论】:
-
内部类、getter、setter ... 你的最后一种语言不会是 Java,对吧? PEP 8 没有解决您的情况,因为它很少出现。你的用例是什么?
-
@StevenRumbalski:实际上,我认为 PEP 8 确实很好地涵盖了这种情况。但是您的更大观点是有效的。很难回答关于语义上空且完全不是 Pythonic 风格的代码的风格问题……
标签: python class properties attributes naming-conventions