这里什么被认为是更好的命名风格?
我正在为 Tkinter 编写一个扩展,并且对于我要添加的一些新属性应该使用哪种命名风格存在冲突。
这是第一种命名风格,为所有新属性赋予前缀 sw_
。
import Tkinter as tk
class New (tk.Button, object) :
def __init__ (self, par) :
tk.Button.__init__(self, par)
attrnames = ['attr0', 'attr1', 'attr2', 'attr3']
for name in attrnames :
newname = 'sw_' + name
setattr(self, newname, None)
root = tk.Tk()
new = New(root)
new.pack()
new.sw_attr0
root.mainloop()
对于第二种命名风格,我创建了 SW 类的所有新 attribute 属性。然后我将 SW
的实例作为新类的属性。
import Tkinter as tk
class SW (object) :
def __init__ (self) :
attrnames = ['attr0', 'attr1', 'attr2', 'attr3']
for name in attrnames :
setattr(self, name, None)
class New (tk.Button, object) :
def __init__ (self, par) :
tk.Button.__init__(self, par)
self.sw = SW()
root = tk.Tk()
new = New(root)
new.pack()
new.sw.attr0
root.mainloop()
I'm writing an extension for Tkinter, and conflicted on which naming style I should use for some new attributes I'm going to add.
This is the first naming style, giving all the new attributes the prefix sw_
.
import Tkinter as tk
class New (tk.Button, object) :
def __init__ (self, par) :
tk.Button.__init__(self, par)
attrnames = ['attr0', 'attr1', 'attr2', 'attr3']
for name in attrnames :
newname = 'sw_' + name
setattr(self, newname, None)
root = tk.Tk()
new = New(root)
new.pack()
new.sw_attr0
root.mainloop()
For the second naming style I made all the new attributes attributes of the SW
class. Then I made an instance of SW
an attribute of my new class.
import Tkinter as tk
class SW (object) :
def __init__ (self) :
attrnames = ['attr0', 'attr1', 'attr2', 'attr3']
for name in attrnames :
setattr(self, name, None)
class New (tk.Button, object) :
def __init__ (self, par) :
tk.Button.__init__(self, par)
self.sw = SW()
root = tk.Tk()
new = New(root)
new.pack()
new.sw.attr0
root.mainloop()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Python 风格指南 是要遵循的 - 做最易读、最明智的事情。也就是说,它确实建议您遵循任何项目样式而不是官方 Python 样式,但在做新事情时使用 Python 样式。
Python 已经将名称空间作为该语言的一项功能(通过导入),因此完全不需要前缀。
在你的情况下,使用前缀是不好的——这意味着你打破了Python的整个鸭子类型意识形态和面向对象的思想。您要求用户知道他们正在使用您的类添加的属性,而不是原来存在的属性,这不是他们关心的。让课堂顺利进行,并避免学习对他们来说不重要的额外内容。它只是一个物体。他们应该只使用它而不关心实施。
Python's style guide is the thing to follow - do the most readable, sensible thing. That said, it does suggest that you follow any project styles over the official Python styles, but to use Python styles when doing something new.
Python already has name-spacing as a feature of the language (with imports), so prefixes are entirely not needed.
In your situation, using prefixes is bad - it means that you break the whole duck-typing ideology of Python and the ideas of object orientation. You require the user to know that they are using attributes added by your class, not originally there, which isn't their concern. Make the class work, and avoid extra stuff that isn't important for them to know. It's just an object. They should just use it without caring about implementation.
唯一的建议是使用信息更丰富的类名,然后是 SW。在 CapCase 中使用完整单词会更好,请参阅 pep 8。
The only recommendation is to use more informative class names, then SW. It's much more better use full words in CapCase, see pep 8.
我认为你不需要任何前缀。
但是使用单独的对象会让使用你的代码的人感到困惑。所以我不得不说,前缀比在混合中引入额外的对象更好。
I don't think you need any prefix.
But using a separate object will serve to confuse people who work with your code. So I have to say that a prefix is better then then introducing an extra object into the the mix.