简体   繁体   English

OOP:Python - 方法调用中的重复值

[英]OOP: Python - repeated values in method call

I'm new to OOP python and trying to understand how to handle instances, I have a method:我是 OOP python 的新手并试图了解如何处理实例,我有一个方法:

class Object:

    things = []

    def __init__(self, table):

        self.table = table
        self.things.append(table)

        ( ... )

    def thingy(self):
        return self.db.execute(f"select date, p1, p2 from {self.table}")

    def all_things(self):
        self.things.extend(
            map(lambda t: Object(thing=t + '_thing').thingy(), Constants.THINGS))

        return self.things 

Now how would I call this object, because my thing is driven by a list from Constants.THINGS , IE: THINGS = ["table1", "table2" ... ] , but in order to create the object to call the method all_things() - I must have a thing set - even tho the method sets the thing on call ...现在我将如何调用这个对象,因为我的东西是由Constants.THINGS的一个列表驱动的,IE: THINGS = ["table1", "table2" ... ] ,但为了创建对象来调用方法all_things() - 我必须设置一个东西 - 即使该方法设置了随叫随到的东西......

This feels a little backward, so would appreciate what it is I am misunderstanding as I think I need to change the constructor/object这感觉有点落后,所以我会理解我误解了什么,因为我认为我需要更改构造函数/对象

a = Object(end_date="2020-01-05",
           start_date="2020-01-01",
           thing=WHAT_TO_PUT_HERE).all_things()

If I add anything to this thing I get a double output如果我在这thing添加任何thing我会得到双重输出

Any help is appreciated任何帮助表示赞赏

  • UPDATE:更新:

The desired output would be that thing() will fire, based on a list input provided by Constants.THINGS , if we input: THINGS = ["table1", "table2"] we would expect thingy() to execute twice with:根据Constants.THINGS提供的列表输入,期望的输出是thing()将触发,如果我们输入: THINGS = ["table1", "table2"]我们希望thingy()执行两次:

select date, p1, p2 from table1 , select date, p1, p2 from table2 select date, p1, p2 from table1select date, p1, p2 from table2

And this would be added to the things class variable, and then when all_things() finishes we should have the content of the two select statements in a list这将被添加到things类变量中,然后当all_things()完成时,我们应该将两个 select 语句的内容放在一个列表中

However,然而,

Object.things

will actually have [WHAT_TO_PUT_HERE, table_1, table2]实际上会有[WHAT_TO_PUT_HERE, table_1, table2]

So according to your update, this is what I think you're attempting to do.所以根据你的更新,这就是我认为你正在尝试做的。

class Object:
    def __init__(self):
        # do some initialization
        pass

    def thingy(self, table):
        return self.db.execute(f"select date, p1, p2 from {table}")

    # call the method "thingy" on all Constants.THINGS
    def all_things(self):
        map(self.thingy, Constants.THINGS)

Then from outside the class you would call it like this.然后在课外你会这样称呼它。

my_instance = Object()
my_instance.all_things()

I'm assuming the class will also have some setup and teardown of your db connection.我假设该课程还将对您的数据库连接进行一些设置和拆卸。 As well as some other things but this is simply a minimalistic attempt at giving an example of how it should work.以及其他一些东西,但这只是一个简单的尝试,给出它应该如何工作的例子。

Okay, so rather than having a class variable which @Axe319 informed me doesn't get reset with every instance as self.table would.好的,而不是拥有@Axe319 通知我的类变量不会像self.table那样在每个实例中重置。 I altered the constructor to just have:我将构造函数更改为仅具有:

class Object:
    def __init__(self, table):
        self.table = table
        self.things = list()

Then when I call the particular method outside the class:然后当我在类外调用特定方法时:

all_things() I can just pass None into the table as the method builds that for me. all_things()我可以将None传递到表中,因为该方法为我构建了它。 ie: IE:

a = Object(thing=None).all_things()

This might be an anti-pattern - again I'm new to OOP, but it's creating something that looks correct.这可能是一种反模式——我还是 OOP 的新手,但它正在创建一些看起来正确的东西。

PS yes I agree, things, thingy, and the thing was a bad choice for variables for this question... PS是的,我同意,事情,事情,对于这个问题的变量来说,事情是一个糟糕的选择......

Thanks谢谢

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

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