python文件命名下划线_关于python:为什么有些函数在函数名之前和之后都有下划线?...

这似乎经常发生,并且想知道这是Python语言中的一个需求,还是仅仅是一个约定的问题?

另外,有人能说出并解释哪些函数往往带有下划线,以及为什么(例如__init__)。

"austinhenley:槽双underscores之前和以后的名字。你的思维underscores只在大学的名称。

相关:"什么是历史原因为什么双underscore辨别的Python类私有成员?"。。。。。。。

一份大学是什么意思是一个单和一双underscore过object name?

为什么会有downvoted如果我问这样一个问题

从python pep 8——python代码样式指南:

Descriptive: Naming Styles

The following special forms using leading or trailing underscores are

recognized (these can generally be combined with any case convention):

_single_leading_underscore: weak"internal use" indicator. E.g. from M import * does not import objects whose name starts with an underscore.

single_trailing_underscore_: used by convention to avoid conflicts with Python keyword, e.g.

Tkinter.Toplevel(master, class_='ClassName')

__double_leading_underscore: when naming a class attribute, invokes name mangling (inside class FooBar, __boo becomes _FooBar__boo; see below).

__double_leading_and_trailing_underscore__:"magic" objects or attributes that live in user-controlled namespaces. E.g. __init__,

__import__ or __file__. Never invent such names; only use them as documented.

请注意,具有双前导和尾随下划线的名称基本上是为Python本身保留的:"永远不要发明这样的名称;只在文档中使用它们"。

Raymond还解释了为什么你希望在视频youtube.com/watch中从大约34分钟开始出现名字错误行为。V= HTLU2DFODTG

因此,一个名字中的单引线下划线和双引线下划线之间的选择有点像C++和Java中的保护和私有之间的选择。_孩子们可以更改单下划线,但是双下划线不能?

__double_leading_underscore仍然是公共的,只需重命名变量以避免冲突。

@johncip感谢视频链接。非常有帮助。

其他受访者正确地将双前导和尾随下划线描述为"特殊"或"魔力"方法的命名约定。

虽然您可以直接调用这些方法(例如,[10, 20].__len__()),但下划线的出现暗示了这些方法将被间接调用(例如,len([10, 20]))。大多数python操作符都有一个关联的"magic"方法(例如,a[x]是调用a.__getitem__(x)的常用方法)。

被双下划线包围的名称对于Python来说是"特殊的"。它们列在Python语言参考第3节"数据模型"中。

实际上,当需要区分父类和子类名称时,我会使用方法名称。我读过一些用这种方式创建父子类的代码。例如,我可以提供以下代码:

class ThreadableMixin:

def start_worker(self):

threading.Thread(target=self.worker).start()

def worker(self):

try:

self._worker()

except tornado.web.HTTPError, e:

self.set_status(e.status_code)

except:

logging.error("_worker problem", exc_info=True)

self.set_status(500)

tornado.ioloop.IOLoop.instance().add_callback(self.async_callback(self.results))

以及有工人方法的孩子

class Handler(tornado.web.RequestHandler, ThreadableMixin):

def _worker(self):

self.res = self.render_string("template.html",

title = _("Title"),

data = self.application.db.query("select ... where object_id=%s", self.object_id)

)

后面双下划线(名称混乱)/来自python文档

Any identifier of the form __spam (at least two leading underscores,

at most one trailing underscore) is textually replaced with

_classname__spam, where classname is the current class name with leading underscore(s) stripped. This mangling is done without regard

to the syntactic position of the identifier, so it can be used to

define class-private instance and class variables, methods, variables

stored in globals, and even variables stored in instances. private to

this class on instances of other classes.

Name mangling is intended to give classes an easy way to define

"private" instance variables and methods, without having to worry

about instance variables defined by derived classes, or mucking with

instance variables by code outside the class. Note that the mangling

rules are designed mostly to avoid accidents; it still is possible for

a determined soul to access or modify a variable that is considered

private.

此约定用于特殊变量或方法(所谓的"magic method"),如_uuu init_uuuuuu,len。这些方法提供特殊的句法特征或做特殊的事情。

例如,file表示python文件的位置,eq在执行a==b表达式时执行。

用户当然可以定制特殊的方法,这是非常罕见的情况,但往往会修改一些内置的特殊方法。(例如,您应该用init初始化类,该init将在创建类实例时首先执行。)

class A:

def __init__(self, a): # use special method '__init__' for initializing

self.a = a

def __custom__(self): # custom special method. you might almost do not use it

pass

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值