[英]Calling one out of multiple variables from method outside or inside of class
I tried looking but couldn't find an answer.我试图寻找但找不到答案。
this is my code -这是我的代码 -
class abcd():
def __init__(self):
pass
def call_from_method_1(self):
self.a = "value of variable a"
self.b = 'value of variable b'
print(self.a + ',', self.b)
def call_from_method_2(self):
self.c = 'var c'
self.d = 'var d'
def call_specific_variable_from_each_method(self):
return abcd().call_from_method_1() # in this line how do I call just the variable b
return abcd().call_from_method_2() # and in this line call just d
if True:
abcd().call_specific_variable_from_each_method() #so I could print them both in this line
I need to call JUST the variable b from def call_from_method_1(self):
and JUST the variable d from def call_from_method_2(self):
and call them into def call_specific_variable_from_each_method(self):
.我需要从
def call_from_method_1(self):
调用变量 b 和从def call_from_method_2(self):
调用变量 d 并将它们调用到def call_specific_variable_from_each_method(self):
。 I also have to keep both methods intact if possible so if I instantiate the methods it also prints out print(self.a + ',', self.b)
from call_from_method_1(self):
which is not desired.如果可能的话,我还必须保持这两种方法的完整性,所以如果我实例化这些方法,它也会从
call_from_method_1(self):
打印出print(self.a + ',', self.b)
): 这是不希望的。
As mentioned both methods have to be intact and still print just a specific variable from either method outside of the class without having to completely run both methods by instantiating them.如前所述,这两种方法都必须是完整的,并且仍然只打印来自 class 之外的任何一种方法的特定变量,而不必通过实例化它们来完全运行这两种方法。
Also as a bonus how do I use if statement in list comprehension to finally call the class?另外作为奖励,我如何在列表理解中使用 if 语句来最终调用 class? so I tried
abcd().call_specific_variable_from_each_method() if True
but doesn't work.所以我尝试了
abcd().call_specific_variable_from_each_method() if True
但不起作用。 Any Ideas?有任何想法吗?
It would really help if the solution is not a workaround.如果解决方案不是解决方法,那将真的很有帮助。 But I'm open to whatever solutions.
但我对任何解决方案都持开放态度。
Your code isn't going to work as-is.您的代码不会按原样工作。
Firstly, you need to instantiate the class abcd
in order to call the methods.首先,您需要实例化 class
abcd
以便调用这些方法。
Secondly, in call_specific_variable_from_each_method(self):
you return the abcd()... calls.其次,在
call_specific_variable_from_each_method(self):
你返回 abcd()... 调用。 They are just going to return None.他们只会返回 None 。 You haven't returned anything in any of the methods.
您没有在任何方法中返回任何内容。 Thirdly, this call returns the first None only.
第三,此调用仅返回第一个 None 。
class abcd():
def __init__(self):
self.call_from_method_1()
self.call_from_method_2()
def call_from_method_1(self):
self.a = "value of variable a"
self.b = 'value of variable b'
return self.b
def call_from_method_2(self):
self.c = 'var c'
self.d = 'var d'
return self.d
def call_specific_variable_from_each_method(self):
return self.call_from_method_1(), self.call_from_method_2()
abcd_obj = abcd()
b, d = abcd_obj.call_specific_variable_from_each_method()
print(b, d)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.