【问题标题】:Double call in python [closed]python中的双重调用[关闭]
【发布时间】:2021-06-13 04:31:05
【问题描述】:

我正在编写这个程序来计算假设商店中商品的总价格,最后我遇到了双重电话。避免这种双重调用的最佳方法是什么?

 #!/usr/bin/env python3
#Lukas Robin
#07.06.2021
class Stonk:
    def __init__(self, name ='', price = '', quantity = '', total = 0, shirtTotal = 0, sockTotal = 0, jeanTotal = 0):
        self.self = self
        self.name = name
        self.price = price
        self.quantity = quantity
        self.total = total
        self.shirtTotal = shirtTotal
        self.sockTotal = sockTotal
        self.jeanTotal = jeanTotal
    def main(__init__):
        __init__.name = ""
        __init__.total = 0
        __init__.shirtTotal = 0
        __init__.sockTotal = 0
        __init__.jeanTotal = 0
        __init__.price = {'shirts' : 25, 'socks': 12, 'jeans' : 50}
        __init__.name = input("Are you buying shirts, socks and/or jeans? ")
    def shirts(main, __init__):
        __init__.quantity = int(input("How many shirts would you like? "))
        __init__.shirtTotal = __init__.shirtTotal + (__init__.quantity*__init__.price['shirts'])
        print("The price of the shirts is, "+ str(__init__.shirtTotal))
        return __init__.shirtTotal
    def socks(main, __init__):
        __init__.quantity = int(input("How many socks would you like? "))
        __init__.sockTotal = __init__.sockTotal + (__init__.quantity*__init__.price['socks'])
        print("The price of the socks is, "+ str(__init__.sockTotal))
        return __init__.sockTotal
    def jeans(main, __init__):
        __init__.quantity = int(input("How many jeans would you like? "))
        __init__.jeanTotal = __init__.jeanTotal + (__init__.quantity*__init__.price['jeans'])
        print("The price of the jeans is, "+ str(__init__.jeanTotal))
        return __init__.jeanTotal
    main(__init__)
    options = __init__.name.split(", ")
    shirtTotals = shirts(main, __init__)
    sockTotals = socks(main, __init__)
    jeanTotals = jeans(main, __init__)
    for items in options:
        if items == ('socks' or 'sock'):
            socks(main, __init__)
        elif items == ('shirts' or 'shirt'):
            shirts(main, __init__)
        elif items == ('jeans' or 'jean'):
            jeans(main, __init__)
        __init__.total = shirtTotals+sockTotals+jeanTotals
        print("Your total is "+str(__init__.total))

(问题已解答,感谢所有帮助过的人。)

【问题讨论】:

  • 您是否有意尝试编写难以理解的代码?你到底为什么要使用__init__main 作为方法的第一个参数的名称??
  • 无论如何,这是否准确地反映了您的缩进?从您的描述中,您的问题完全不清楚。请参阅How to Askhelp center
  • 对不起,如果程序难以理解,我是python的初学者。问题出在第 43-50 行的某个地方,函数被调用了两次,我想知道是否有任何方法可以解决这个问题?
  • 这不是一个充分的问题规范。 再次请参阅我上面链接的资源,以帮助编写更清晰的问题
  • @RustyB 我建议不要使用 w3schools。不是很好的资源。最好去official tutorial/documentation

标签: python python-3.x oop


【解决方案1】:

你的问题在这里:

options = __init__.name.split(", ")
shirtTotals = shirts(main, __init__)
sockTotals = socks(main, __init__)
jeanTotals = jeans(main, __init__)

shirts() socks()jeans() 无论input("Are you buying shirts, socks and/or jeans? ") 的结果如何都会被调用

所以我要做的第一个修复是删除shirtssocksjeans的重复项

所以代码的底部应该是这样的:

main(__init__)
options = __init__.name.split(", ")
for items in options:
    if items == ('socks' or 'sock'):
        socks(main, __init__)
    elif items == ('shirts' or 'shirt'):
        shirts(main, __init__)
    elif items == ('jeans' or 'jean'):
        jeans(main, __init__)
    __init__.total = shirtTotals+sockTotals+jeanTotals

print("Your total is "+str(__init__.total))

即使有了这些修复,dunder 方法仍然存在误用。

邓德法

Dunder 方法或双下划线方法是不打算直接调用的特殊或魔术方法。 __init__ 方法就是一个例子。

如果我们要创建一个新的 Stonk 对象,我们不直接调用 __init__,而是调用 Stonk(),然后 python 将在幕后调用 __init__,这样可以保持代码更简洁,因为我们没有 init函数调用散布在代码中,而我们看到的是更清晰的 Stonk()。

因此,我们必须从每个不属于它的地方删除__init__

相反,我们应该将其替换为引用当前对象的self

通过这些更改,您的代码将如下所示:

class Stonk:

    def __init__(self):
        self.name = ""
        self.total = 0
        self.shirtTotal = 0
        self.sockTotal = 0
        self.jeanTotal = 0
        self.price = {'shirts': 25, 'socks': 12, 'jeans': 50}
        self.name = input("Are you buying shirts, socks and/or jeans? ")

    def shirts(self):
        quantity = int(input("How many shirts would you like? "))
        self.shirtTotal = self.shirtTotal + (quantity * self.price['shirts'])
        print("The price of the shirts is, "+ str(self.shirtTotal))
        return self.shirtTotal

    def socks(self):
        quantity = int(input("How many socks would you like? "))
        self.sockTotal = self.sockTotal + (quantity * self.price['socks'])
        print("The price of the socks is, " + str(self.sockTotal))
        return self.sockTotal

    def jeans(self):
        quantity = int(input("How many jeans would you like? "))
        self.jeanTotal = self.jeanTotal + (quantity * self.price['jeans'])
        print("The price of the jeans is, " + str(self.jeanTotal))
        return self.jeanTotal


stonk = Stonk() # creates new Stonk object

options = stonk.name.split(", ")

for items in options:
    if items == ('socks' or 'sock'):
        stonk.socks()
    elif items == ('shirts' or 'shirt'):
        stonk.shirts()
    elif items == ('jeans' or 'jean'):
        stonk.jeans()
    stonk.total = stonk.shirtTotals + stonk.sockTotals + stonk.jeanTotals
    print("Your total is "+str(stonk.total))

但是我们得到了一个例外! AttributeError: 'Stonk' object has no attribute 'shirtTotals'

如果我们比较以下代码块:

def socks(self):
    quantity = int(input("How many socks would you like? "))
    self.sockTotal = self.sockTotal + (quantity * self.price['socks'])
    print("The price of the socks is, " + str(self.sockTotal))
    return self.sockTotal

和:

for items in options:
    if items == ('socks' or 'sock'):
        stonk.socks()
    elif items == ('shirts' or 'shirt'):
        stonk.shirts()
    elif items == ('jeans' or 'jean'):
        stonk.jeans()
    stonk.total = stonk.shirtTotals + stonk.sockTotals + stonk.jeanTotals

我们可以看到shirtTotalsshirtTotal不匹配,sockTotalsjeanTotals也存在同样的问题。这里的解决方法是将 Totals 更改为 Total

我们可以做的另一个改进是从构造函数中取出读取输入。这是因为在构造函数中读取输入是不直观的。通过这 2 项改进,您的代码将如下所示:

class Stonk:

    def __init__(self):
        self.name = ""
        self.total = 0
        self.shirtTotal = 0
        self.sockTotal = 0
        self.jeanTotal = 0
        self.price = {'shirts': 25, 'socks': 12, 'jeans': 50}

    def shirts(self):
        quantity = int(input("How many shirts would you like? "))
        self.shirtTotal = self.shirtTotal + (quantity * self.price['shirts'])
        print("The price of the shirts is, "+ str(self.shirtTotal))
        return self.shirtTotal

    def socks(self):
        quantity = int(input("How many socks would you like? "))
        self.sockTotal = self.sockTotal + (quantity * self.price['socks'])
        print("The price of the socks is, " + str(self.sockTotal))
        return self.sockTotal

    def jeans(self):
        quantity = int(input("How many jeans would you like? "))
        self.jeanTotal = self.jeanTotal + (quantity * self.price['jeans'])
        print("The price of the jeans is, " + str(self.jeanTotal))
        return self.jeanTotal

stonk = Stonk() # creates new Stonk object

options = input("Are you buying shirts, socks and/or jeans? ").split(", ")

for items in options:
    if items == ('socks' or 'sock'):
        stonk.socks()
    elif items == ('shirts' or 'shirt'):
        stonk.shirts()
    elif items == ('jeans' or 'jean'):
        stonk.jeans()

stonk.total = stonk.shirtTotal + stonk.sockTotal + stonk.jeanTotal
print("Your total is "+str(stonk.total))

通过这些小修复we can see your code running here

我会考虑进行的修改列表:

【讨论】:

  • 这超出了职责范围。您应该获得“超级教师”徽章。
  • 老兄,非常感谢,这太棒了。我编辑过,效果很好。非常感谢!
  • 乐于助人,学习一门新语言很容易被绊倒。当我拿起python时,Dunder给我带来了麻烦。请记住 __init__ 是一个构造函数。如果您有任何问题或更改,请随时提出。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-22
  • 1970-01-01
  • 2021-10-25
  • 2016-12-09
  • 2010-09-08
  • 1970-01-01
相关资源
最近更新 更多