简体   繁体   English

使用 self 参数调用 python 方法

[英]Call python method with self parameter

He guys, I'm new with Python and now I'm studying Object oriented with py.伙计们,我是 Python 的新手,现在我正在研究面向 py 的 Object。 So, in my case I have a class Founds with one atributte (it will increase later), and I'm modeling to right standards.所以,就我而言,我有一个 class Founds 有一个属性(稍后会增加),我正在按照正确的标准建模。 But when I call this method from another class (Main), it throws:但是当我从另一个 class (主)调用此方法时,它会抛出:

TypeError: val() missing 1 required positional argument: 'fundos' 

My main class where I call, instantiate this method.我调用的主要 class 实例化此方法。 I prefer to use objects, because system will grow up in a few days in complexity.我更喜欢使用对象,因为系统会在几天内变得复杂。 My main:我的主要:

class Main(FlaskForm):   

    series = None 
    fundos = fundos()

    @app.route('/resultado', methods=['GET','POST'])
    def resultado():
        titulo = "Retorno da pesquisa"
       
        valuation = request.form.getlist("fundo_select")
        nomefundo = request.form.get("fii")
        fundoss = any
        fundoss = nomefundo.split(',')  
            
        **series = fundos.val(fundoss)**            

My founds class:我的创立 class:

class Fundos(object):  
    # Atribuindo na classe como Nada
    lista_fiis=None;

    # Array de atributos da classe, inicializando
    def __init__(self):
        # Inicializando como array
        self.lista_fiis=[]


    # Pega fundos selecionados
    # Gera o relatório no excel
    def val(self, fundos):
        info_fii={}

In Python, unlike in Java or C++, where this.在 Python 中,与在 Java 或 C++ 中不同,这里this. or this-> can be implied, you cannot drop self.或者this->可以被暗示,你不能放弃self. when accessing member properties.访问成员属性时。 Therefore, when you use fundos.val(fundoss) , you are not using the fundos = fundos() variable defined on the class, but instead using the class itself.因此,当您使用fundos.val(fundoss)时,您没有使用class 上定义的fundos = fundos()变量,而是使用class 本身。

Another thing you should know about OOP and methods in Python is that you can access any method defined on the class as though it was a static method; Another thing you should know about OOP and methods in Python is that you can access any method defined on the class as though it was a static method; you just have to provide it the instance yourself.您只需要自己提供实例即可。 In other words, given the following class:换句话说,给定以下 class:

class MyClass:
    hello(self):
        print('Hello!')

myInstance = MyClass()

calling myInstance.hello() is the same as calling MyClass.hello(myInstance) .调用myInstance.hello()与调用MyClass.hello(myInstance)相同。

So, what you are doing in your call is in effect calling the method directly from the class, providing it fundoss as the self parameter and not providing the second parameter.因此,您在调用中所做的实际上是直接从 class 调用该方法,将它作为self参数提供fundoss ,而不提供第二个参数。

What you need to do is use self.fundos.val(fundoss) to use the instance you created for Main rather than the class fundos .您需要做的是使用self.fundos.val(fundoss)来使用您为Main而不是 class fundos创建的实例。

PS: Generally, it is a good idea to stay away from name shadowing and not use the same name for your class and instance, then these problems will be a lot easier to spot PS:一般来说,最好远离名称阴影,不要为您的 class 和实例使用相同的名称,那么这些问题将更容易发现

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

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