【问题标题】:How to call a __private static method from other methods in python如何从python中的其他方法调用__private静态方法
【发布时间】:2018-04-18 10:14:11
【问题描述】:

我假设 Python 类中的私有静态方法是可以而且应该做的事情。但也许实际上,我应该只是在类外使用模块私有方法。

我想了解从不同位置调用不同种类的静态方法:

我有一个带有私有和公共静态方法的 Python 类。我想从其他地方给他们打电话,从对方那里打电话。

在类外调用公共静态方法时,我必须添加类名。即

m = MyClass.the_staticmethod(100) # I must use the classname as a prefix

查看代码中的问题:

    class Myclass():
        @staticmethod
        __my_privatestaticmethod(myparam):
             return myparam

        @staticmethod
        def the_staticmethod(myparam):
            # will the following work?
            result = __my_staticmethod(1) # will this work?

            # data-mingling set as private, so following line cannot work!
            result = Myclass.__my_staticmethod(2) # this cannot work. 

            result = the_staticmethod(3) # will this work without the prefix

            return result
 

        def __my_privatemethod(self, param1):
            # which of the following are valid?
            return __my_staticmethod(11) # will this work?

            # data-mingling set as private, so following line cannot work!
            return Myclass.__my_staticmethod(12) # this cannot work. 

            return the_staticmethod(13) # will this work without the prefix of the class? 
            
            return self.the_staticmethod(14) # will this work. Is the self also considered the class? 

            return  Myclass.the_staticmethod(15) # this of course works. 
            

        def the_method(param1):
            return __my_staticmethod(param1) # will this work?

如果 1 和 11 的答案是否定的,那么结论是你不能创建私有静态方法。

然后我会在没有装饰器的类之外创建一个私有模块方法。这相当于私有静态类方法。

    def __my_privatemodulemethod(param1):
         return param1

并且可以从我的模块中的任何位置调用它,无需前缀。

【问题讨论】:

  • Python 没有私有与公共类元素。如果存在,可以引用。
  • Python 不是 C#。我想你想要达到的目标有更多 pythonic 等价物。
  • 我不明白你的问题是什么。您可以通过尝试逐个运行它们来找出其中哪些调用有效;你需要我们做什么?
  • staticmethod 是一种不接收 clsself 参数的方法。如果您确实想访问同一类/实例的其他方法,那么该方法确实不应该是static
  • 是的,这就是为什么我明确使用了__private这个词,并在示例中使用了dunder来进行数据混合。

标签: python class methods static


【解决方案1】:

正如 deceze 在评论中已经提到的那样,在 Python 中,staticmethod 是一种不将实例作为第一个参数的方法。由于 Python 没有隐式的this 指针,显然一个staticmethod 没有办法引用当前类,所以它不能在当前类上调用另一个staticmethod。这里明显的解决方案是改用classmethods(classmethods 将当前类作为第一个参数):

class Foo(object):
    @classmethod
    def _protected(cls, arg):
        print("{}._protected() called with {}".format(cls.__name__, arg))

    @classmethod
    def public(cls, arg):
        cls._protected(arg)

通过数据混合实现了私有/公共的概念

s/数据混合/名称修改/g ;)

“dunder”名称和名称修改机制不会将任何内容设为私有:

class Foo(object):
    @staticmethod
    def __not_private():
        print("peek a boo")


Foo._Foo_not_private()

As the "clunky" doc states,这里的重点主要是为了避免意外覆盖基类的某些重要实现部分。实际上,这很少使用,而且大多数时候甚至不需要。表示“实现”方法和属性的约定是用 single 前导下划线命名它们。

附带说明一下,关于 Python 文档质量的刻薄评论不会让您对朋友有太多帮助。

【讨论】:

  • 谢谢,布鲁诺。所以 Python 程序员大多是 Python 爱好者,他们不喜欢任何关于文档及其呈现方式的评论?对不起!!我没有冒犯的意思。我认真地认为,任何来自较新的 c 语言(pascal、dotnet,尤其是 c#、java 等)的任何人浏览此文档(有点过时,缺少每个参数及其用法的内部链接和示例)都会觉得很难了解如何编写代码。
  • @pashute 我是通过官方文档学习 Python 的,但从未发现它“笨拙”。但是如果你认为你可以改进它,请贡献 - 这是一个免费的开源项目。
  • 有什么地方可以发表关于这个的建议吗?是否有数百名程序员将他们的示例和链接添加到每个选项和功能?
  • 好的,谢谢。订阅并将尝试让事情进展。
猜你喜欢
  • 1970-01-01
  • 2017-10-08
  • 2019-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-28
  • 1970-01-01
  • 2013-01-12
相关资源
最近更新 更多