简体   繁体   English

访问指向对象的类中的成员变量的方法的正确方法是什么?

[英]What's the correct way to access methods of a member variable in a class pointing to an object?

What's the correct way to define/access methods of a member variable in a class pointing to an object? 在指向对象的类中定义/访问成员变量的方法的正确方法是什么?

For example, I have a class Foo that has a method foo_method : 例如,我有一个具有方法foo_method Foo类:

class Foo:
    def foo_method(self):
        return 'bar'

Now, in another class Bar , I'd like to store an object to this class (I do not want Bar to inherit Foo ). 现在,在另一个类Bar ,我想将一个对象存储到此类(我不希望Bar继承Foo )。

What is the correct/recommended way to define class Bar ? 定义Bar类的正确/推荐方法是什么?

class Bar:
    def __init__(self):
        self.foo = Foo()

# OR

class Bar:
    def __init__(self):
        self.foo = Foo()

    def bar_method(self):
        return self.foo.bar()

The former will allow me to use: 前者将允许我使用:

x = Bar()
x.foo.foo_method()

But, with this I'm directly accessing methods of a member variable (strong coupling). 但是,与此同时,我直接访问成员变量的方法(强耦合)。

The benefits I see with this approach are: 我看到的这种方法的好处是:

  • I don't have to wrap every new method that gets added to class Foo . 我不必包装添加到Foo类的每个新方法。
  • Bar may contain more member variables pointing to other classes. Bar可能包含更多指向其他类的成员变量。 Not wrapping them keeps Bar smaller and manageable in size. 不包裹它们可使Bar变得更小且易于管理。
  • The auto-completion facility from IDE (PyCharm, etc.) or IPython helps inspect bar like a menu ( x.foo ) followed by a sub-menu ( x.foo.foo_method() , x.bar.foobar() , etc.) making it easier to develop code. 从IDE(PyCharm等)或IPython的自动补全功能帮助检查bar就像一个菜单( x.foo ),后跟一个子菜单( x.foo.foo_method() x.bar.foobar()等。)使其更易于开发代码。
  • Functional programming look-n-feel (not sure if this a pro or con) 函数式编程看起来不错(不确定是赞成还是反对)

The cons are strong coupling, not encapsulating internal details of foo , etc. 缺点是很强的耦合,没有封装foo等内部细节。

I wanted to check if this a recommended practice? 我想检查一下这是否是推荐做法? And/or if there are any guidelines related to this (kind of implementing a composite pattern)? 和/或是否有与此相关的准则(实施复合模式)?

Any inputs/pointers will be highly appreciated! 任何输入/指针将不胜感激!

There are no real guidelines, but if you make a bar_method() that explicitly calls foo.bar() and does nothing in between it's the exact same coupling and less convenient. 没有真正的指导原则,但是如果您创建一个bar_method()来显式调用foo.bar()且在两者之间不执行任何操作,则这是完全相同的耦合,并且不太方便。

Python's idioms include " We are all responsible users " and in that case, when encapsulation does not provide you any benefit, I personally wouldn't use proxy functions. Python的习惯用法包括“ 我们都是负责任的用户 ”,在这种情况下,如果封装没有给您带来任何好处,我个人就不会使用代理功能。 Leaving it as is results in smaller code which is easier to maintain and is more accessible. 保留原样将导致更小的代码,更易于维护和访问。 Auto-completion as you mentioned is just an added bonus. 您提到的自动完成只是一项额外的奖励。

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

相关问题 在Python中,从变量实例化类的正确方法是什么? - In Python, what's the correct way to instantiate a class from a variable? 如果可以的话,在其中一个组件内访问 class 复合材料的正确方法是什么? - What is the correct way to access a class composite inside one of its components, if that's even advisable? 指向 class 而不创建 object 以便于访问 - Pointing to a class without creating an object for ease of access 访问对象中的类变量的最佳方法? - Best way to access a class variable in an object? `global`是类访问模块级变量的正确方法吗? - Is `global` the correct way for a class to access a module level variable? 减少函数内变量的正确方法是什么? - What's the correct way to decrease a variable within a function? 设计调用其方法序列的 class 的最佳方法是什么? - What's the best way to design a class that calls sequences of its methods? Python-在另一个类中对方法进行单元测试的正确方法是什么? - Python - What's the proper way to unittest methods in a different class? 实例方法中类成员变量的可见性 - Class member variable visibility in instance methods 在这个 class 中测试这个“setUp()”function 的正确方法是什么? - What's the correct way to test this “setUp()” function inside this class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM