Python:调用其“兄弟”的公共方法私有方法

发布于 2024-08-09 06:26:13 字数 871 浏览 3 评论 0原文

我编写 Python 代码才几周时间,所以我仍在摸索情况。但是,假设我有一个方法,有时可能会被“用户”调用,并且会在内部大量使用(即,在调用之前已经检查了参数)。这就是我目前正在做的事情:

#The method the 'user' should call:
def do_something(self, arg1, arg2, arg3):
    #write code to do error checking on arg1, agr2, arg3
    #raise exceptions, return codes, etc: depends on whether you are an explicit lover
    #or an implicit lover, it seems. :-)
    ... error checking code here...
    #Now call the 'brother' method that does the real work.
    return self._do_something(self, arg1, arg2, arg3, arg3)

#The method other private methods should call with already validated parameters
def _do_something(self, arg1, arg2, arg3, arg3):
    #don't do error checking on the parameters. get to work...
    ... do what you do...
    return whatever you're supposed to return

这对我来说似乎是合乎逻辑的。有没有更好的 Python 风格的方法来做到这一点?

保罗

I have been writing Python code for only a couple of weeks, so I'm still figuring out the lay of the land. But let's say I have a method that MAY be called on by a 'user' on occasion as well as used HEAVILY internally (ie, the arguments have already been checked before the call). Here is what I am currently doing:

#The method the 'user' should call:
def do_something(self, arg1, arg2, arg3):
    #write code to do error checking on arg1, agr2, arg3
    #raise exceptions, return codes, etc: depends on whether you are an explicit lover
    #or an implicit lover, it seems. :-)
    ... error checking code here...
    #Now call the 'brother' method that does the real work.
    return self._do_something(self, arg1, arg2, arg3, arg3)

#The method other private methods should call with already validated parameters
def _do_something(self, arg1, arg2, arg3, arg3):
    #don't do error checking on the parameters. get to work...
    ... do what you do...
    return whatever you're supposed to return

This seems logical to me. Is there a better Python-ish way to do this?

Paul

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

温暖的光 2024-08-16 06:26:13

那很好。不过,您的代码中对“兄弟”方法的调用是错误的。您应该这样做:

# Now call the 'brother' method that does the real work.
return self._do_something(arg1, arg2, arg3, arg3)

也就是说,您应该“通过”自引用来调用它,因为它是一个对象方法而不是全局函数。

That is fine. The call to the "brother" method is wrong in your code, though. You should do it like this:

# Now call the 'brother' method that does the real work.
return self._do_something(arg1, arg2, arg3, arg3)

That is, you should call it "through" the self reference, since it's an object method and not a global function.

生来就爱笑 2024-08-16 06:26:13

python 中没有对私有成员的“真正”支持,但将成员指示为私有的 pythonic 方法是使用两个前导下划线。就您而言,__do_something

有关更多详细信息,请参阅 python.org - 类

There is no "true" support for private members in python, but the pythonic way to indicate a member as private is to use two leading underscores. In your case, __do_something.

For further details, see python.org - classes

千仐 2024-08-16 06:26:13

好吧,除非错误检查代码非常昂贵,否则我只有一种方法,它总是进行错误检查。它可能会重复一些检查,但它确实为您提供了更多的安全性,并且如果有人继承了您的类,它可能会派上用场。

如果稍后您需要性能,您可以随时缓存结果或执行其他操作。

well, unless the error checking code is very expensive, I would have only one method, which always does the error checking. It may repeat some checks, but it does offer you more security, and it may come in handy if someone inherits from your class.

If later on you need performance you can always cache results or do something else.

魂ガ小子 2024-08-16 06:26:13

我自己正在学习 python(并且很享受),但我认为这就是实现它的方法。
但是,私有方法应该有两个下划线,并像“self.__do_something()”那样调用。

I'm just learning python myself (and enjoying it) but I think that's the way to do it.
However, the private method should have two underscores and called like 'self.__do_something()'.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文