简体   繁体   English

使用Main()在函数之间传递数据

[英]Passing data between functions, using Main()

I am trying to pass data from one function to another, with in a class and print it. 我试图在一个类中将数据从一个函数传递到另一个函数并将其打印出来。 I am unsuccessful and keep getting errors. 我不成功,并不断出错。 Error at bottom for help. 错误底部,以寻求帮助。 Thanks in advance. 提前致谢。

class Stocks(object):
def __init__(self, name):
    self.name = name
    print(name)

def Get_Data(self):
    #self.data2 = data2
    #print(self.name)
    data2 = web.get_data_yahoo(self.name,data_source='yahoo',start=start_date,end=end_date)['Adj Close']
    #print(data2)
    #data2.plot(figsize=(10,5))
    #plt.show()
    return data2

def Main(self, Get_Data):

    x = Stocks(Get_Data())
    print(x)
    #data2.plot(figsize=(10,5))
    #plt.show()

z = Stocks('GE')
z.Get_Data()
z.Main()


error:
TypeError: Main() missing 1 required positional argument: 'Get_Data'
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-162-91f08c3bdb57> in <module>()
     32 z = Stocks('GE')
     33 z.Get_Data()
---> 34 z.Main()
TypeError: Main() missing 1 required positional argument: 'Get_Data'

A function definition includes a list of parameters, that have to be matched by the arguments passed in when the function is called. 函数定义包括参数列表,这些参数必须与调用函数时传递的参数匹配。

So, if you define Main to take a Get_Data parameter, you have to pass something an argument to be the value for Get_Data whenever you call it. 因此,如果将Main定义为采用Get_Data参数, Get_Data每次调用它时,都必须传递一个参数作为Get_Data的值。

But you're not passing anything at all, hence the TypeError . 但是您根本没有传递任何东西,因此没有传递TypeError


Since Main expects to call its Get_Data with no arguments, it has to be something callable with no arguments. 由于Main希望Get_Data带任何参数地调用其Get_Data ,因此它必须是可带任何参数的可调用对象。 The bound method z.Get_Data fits the bill, so you could do this: 绑定方法z.Get_Data符合要求,因此您可以执行以下操作:

z.Main(z.Get_Data)

But that's a very weird design. 但这是一个非常奇怪的设计。


More likely, you didn't want to add such a parameter in the first place: 您更有可能不想一开始就添加这样的参数:

def Main(self):

… because what you want Main to do is call Get_Data on itself, not to call some arbitrary thing the caller passed in: …因为Main要做的是自己调用Get_Data ,而不是调用方传入的任意操作:

x = Stocks(self.Get_Data())
print(x)

Except that you probably didn't want to construct a new Stocks value here, with the result of z.Get_Data() as the name, you just wanted to use the result of z.Get_Data() . 除了您可能不想在这里构造一个新的Stocks值(以z.Get_Data()的结果作为名称)之外,您只想使用z.Get_Data()的结果。 So: 所以:

x = self.Get_Data()
print(x)

The error is quite obvious: you declared Main to have a parameter, Get_Data (ignoring the self pointer that all class members implicitly have/pass). 错误非常明显:您声明Main具有参数Get_Data (忽略所有类成员隐式拥有/传递的self指针)。 When you call z.Main() you are not passing that parameter, so the interpreter asks you to fill this gap. 当您调用z.Main()您没有传递该参数,因此解释器要求您填补这一空白。

You probably meant something like: 您可能的意思是:

def Main(self):
    x = Stocks(self.Get_Data())
    print(x)

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

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