简体   繁体   English

试图弄清楚如何在从字典调用 class 时将变量从一个 class 传递到另一个 python

[英]Trying to figure out how to pass variables from one class to another in python while calling a class from a dictionary

So I am getting used to working with OOP in python, it has been a bumpy road but so far things seem to be working.所以我习惯了在 python 中与 OOP 一起工作,这是一条崎岖不平的道路,但到目前为止一切似乎都在运作。 I have, however hit a snag and i cannot seem to figure this out.但是,我遇到了障碍,我似乎无法弄清楚。 here is the premise.这是前提。

I call a class and pass 2 variables to it, a report and location.我调用 class 并将 2 个变量传递给它,一个报告和位置。 From there, I need to take the location variable, pass it to a database and get a list of filters it is supposed to run through, and this is done through a dictionary call.从那里,我需要获取位置变量,将其传递给数据库并获取它应该运行的过滤器列表,这是通过字典调用完成的。 Finally, once that dictionary call happens, i need to take that report and run it through the filters.最后,一旦字典调用发生,我需要获取该报告并通过过滤器运行它。 here is the code i have.这是我的代码。

class Filters(object):

    def __init__ (self, report, location):
        self.report = report
        self.location = location

    def get_location(self):
        return self.location

    def run(self):
        cursor = con.cursor()
        filters = cursor.execute(filterqry).fetchall()
        for i in filters:
            f = ReportFilters.fd.get(i[0])
            f.run()
        cursor.close()


class Filter1(Filters):

    def __init__(self):
    self.f1 = None
   ''' here is where i tried super() and Filters.__init__.() etc....  but couldn't make it work'''

    def run(self):
    '''Here is where i want to run the filters but as of now i am trying to print out the 
     location and the report to see if it gets the variables.'''

    print(Filters.get_location())  

class ReportFilters(Filters):

    fd = {
    'filter_1': Filter1(),
    'filter_2': Filter2(),
    'filter_3': Filter3()
    }

My errors come from the dictionary call, as when i tried to call it as it is asking for the report and location variables.我的错误来自字典调用,因为当我试图调用它时它要求报告和位置变量。

Hope this is clear enough for you to help out with, as always it is duly appreciated.希望这对您来说足够清楚,可以一如既往地得到应有的赞赏。

DamnGroundHog该死的土拨鼠

The call to its parent class should be defined inside the init function and you should pass the arguments 'self', 'report' and 'location' into init () and Filters.对其父 class 的调用应该在 init function 中定义,您应该将 arguments 的“self”、“report”和“location”传递给init () 和 Filters。 init () call to parent class so that it can find those variables. init () 调用父 class 以便它可以找到那些变量。

If the error is in the Filters1 class object, when you try to use run method and you do not see a location or a report variable passed in from parent class, that is because you haven't defined them when you instantiated those object in ReportFilters.fd如果错误在 Filters1 class object 中,当您尝试使用 run 方法时,您没有看到从父 class 传入的位置或报告变量,那是因为您在 ReportFilters 中实例化那些 object 时没有定义它们.fd

It should be:它应该是:

class ReportFilters(Filters):
    fd = {
        'filter_1': Filter1(report1, location1),
        'filter_2': Filter2(report2, location2),
        'filter_3': Filter3(report3, location3)
    }


class Filter1(Filters):
    def __init__(self, report, location):
        Filters.__init__(self, report, location)
        self.f1 = None

    def run(self):
        print(self.get_location())

暂无
暂无

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

相关问题 如何将变量从一个 class 传递到另一个 class python? - How to pass variable from one class to another class python? Python 3:将变量从一个CLASS传递到另一个 - Python 3: Pass variable from one CLASS to another 从 class 外部或内部的方法调用多个变量中的一个 - Calling one out of multiple variables from method outside or inside of class 如何将变量从一个 class 传递到 Python 中的另一个变量? - How can I pass a variable from one class to another in Python? 在Python中如何将相同的属性从一个类传递到另一个类 - In Python how to pass and set the same properties from one class to another 如何将变量值从一个文件中的一个类传递到另一个文件中的另一个类 python tkinter - How to pass variable value from one class in one file to another class in another file python tkinter Python / Kivy:将变量从一类传递到另一类 - Python/Kivy : pass a variable from one class to another class Python/Kivy - 将变量从一个 class 发送到另一个 - Python/Kivy - Sending variables from one class to another Python - 从一个 class 获取列表/字典,在另一个 class 中进行修改并将修改值返回给第一个 ZA2F2ED4F8EBC26BBCB1 - Python - Get list/dictionary from one class, do the modification in another class and return the modify value to first class 如何在 python 中从一个 class 到另一个 class 获取变量 - How to get variable from one class to another class in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM