简体   繁体   English

从python中的继承类访问私有方法

[英]Accessing private methods from inherited class in python

I want to follow good programming practice, so i am kind of stuck at this questions: 我想遵循良好的编程习惯,所以我有点被这个问题困扰:

Lets say i have Class root, 可以说我有Class Root,

Class root(Object):
   def __init__(self):
     self._root_tree = 'Base'

   def __str__(self):
     return self._root_tree

   def _test(self):
      return 'test'

lets say i create a class called Oak 可以说我创建了一个名为Oak的类

Class Oak(root):
    def __str__(self):
      return 'Oak'
    def _test(self):
      return 'Oak_test'
    def _new_fun(self):
      return 'new_func_only_in_oak'

Then in Class Cherry, can i do the following 然后在课堂樱桃中,我可以做以下事情吗

Class Cherry(root):
    def _grab_trees(self,another_tree): #another_tree is a Oak object
      other_tree = another_tree.__str__() #this will return Oak
      return 'The other three is: ' + other_tree
    def _test2(self,another_tree):
      return another_tree._test()
    def _testing_new(self,another_tree):
      return another_tree._new_fun()

Basically calling __str__() _new_fun() and _test() in the Cherry class valid (good practice). 基本上调用__str__() _new_fun()_test()在樱花类有效的(好习惯)。

You can, but in this example it would be better to just call str(another_tree) . 您可以,但是在此示例中,最好只调用str(another_tree) In general you shouldn't call the double-underscore methods directly unless you're doing something that specifically relates to their implementation (eg, calling a superclass implementation from within a subclass implementation). 通常,除非您要进行与它们的实现特别相关的操作(例如,从子类实现中调用超类实现),否则不应直接调用double-underscore方法。 Mostly the double-underscore methods exist to let the class behave in a certain way in a more "normal" context, and you should use that normal context for normal purposes. 通常,双下划线方法的存在是为了让类在更“正常”的上下文中以某种方式运行,并且您应出于正常目的使用该正常上下文。 For instance, __str__ exists to let the class do the right thing when you call str() on it, so you should call str() on it instead of calling __str__ directly. 例如,存在__str__以便在类上调用str()时让类做正确的事情,因此应在其上调用str()而不是直接调用__str__

As for single-underscore methods, it's generally not a good idea to call those unless you wrote them. 至于单下划线方法,除非您编写了这些方法,否则通常不建议调用这些方法。 There's no "penalty" for doing so, but by convention the single underscore indicates methods that aren't part of a public API. 这样做没有“惩罚性”,但是按照惯例,单个下划线表示不属于公共API的方法。 So if you use them, you run the risk of having your code break if the library you're using is updated and those underscore methods disappear or change. 因此,如果使用它们,则如果所使用的库已更新并且那些下划线方法消失或更改,则冒着代码中断的风险。

It depends on what your design declares as the logical interface for the methods of these classes. 这取决于您的设计声明为这些类的方法的逻辑接口。

If you somehow, somewhere specify that you are returning non-instance then what you do is perfectly fine. 如果您以某种方式在某处指定要返回非实例,那么您所做的一切就很好。 If you are creating implicit expectation that method returns an instance of Cherry and you are returning Oak then you are introducing room for serious bugs. 如果创建隐式期望,则该方法将返回Cherry的实例,而您将返回Oak,则将为严重的bug引入空间。

In general, reserve the __method__ convention for the inner workings of your class and for recasting. 通常,为类的内部工作和重铸保留__method__约定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM