如何在python中的另一个方法中调用类方法? [英] How to call a class method in another method in python?

查看:1575
本文介绍了如何在python中的另一个方法中调用类方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试打印好,谢谢".当我在shell上运行它时,它在单独的行上打印,并且在确定"之前打印感谢"字样.谁能帮助我做错事情?

I am trying to print 'okay, thanks'. When I run it on shell, it prints on separate line and the 'thanks' is printing before 'okay'. Can anyone help what I am doing wrong?

>>> test1 = Two() 
>>> test1.b('abcd') 
>>> thanks 
>>> okay

我的代码

class One:
     def a(self):
         print('thanks')

class Two:
     def b(self, test):
         test = One()
         print('okay', end = test.a())

推荐答案

我会尝试这样的操作,因为这意味着您不必更改类One.这减少了您必须更改的类的数量,从而隔离了更改和错误的范围;并保持One类的行为

I'd try something like this as it means you don't have to change class One. This reduces the amount of classes you have to change, which isolates the change and the scope for error; and maintains the behaviour of class One

class One:
     def a(self):
         print('thanks')

class Two:
     def b(self, test):
         test = One()
         print('okay', end=' ')
         test.a()

这篇关于如何在python中的另一个方法中调用类方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆