将函数结果作为输入参数传递给类中的函数

2024-05-28 18:57:22 发布

您现在位置:Python中文网/ 问答频道 /正文

 class main_prg:
      def func():
            # code that generates x and y
            return x,y
      def func1(x):
            #uses the value of x that is returned from func()
           z=x+3
            return z

如何将x传递给func1()

我试过了

m=main_prg()
f01,f11 = m.func()
f2 = m.func1(f01)

向我抛出一个错误func1() takes exactly 1 argument (2 given)


Tags: andthereturnthatvaluemaindefcode
1条回答
网友
1楼 · 发布于 2024-05-28 18:57:22

这会给你你想要的输出。必须向类中的每个函数添加self

class main_prg:
      def func(self):
            # code that generates x and y
            x = 1 # whatever
            y = 2 # whatever
            return x,y
      def func1(self, x):
            #uses the value of x that is returned from func()
            z = 3 # whatever
            return z

m = main_prg()
f01,f11 = m.func()
print f01
print f11
f2 = m.func1(f01)
print f2
  • f01打印1
  • f11打印2
  • f2打印3

相关问题 更多 >

    热门问题