【发布时间】:2018-02-01 04:15:58
【问题描述】:
class Gathering(object):
def __init__(self, date, spent1, spent2, spent3, spent4):
"""Return a gathering object whose date is declared """
self.date = date
self.spent1 = spent1
self.spent2 = spent2
self.spent3 = spent3
self.spent4 = spent4
self.spent_total = spent1+spent2+spent3+spent4
def per_person(self):
return self.spent_total/3
我写了一个简短的脚本,当我和我的朋友有一些东西时,我可以很容易地计算出一个人的部分。我们通常会搬家,花的钱不一样,但那天晚上我们去了多少地方总是不一样的。
所以我想将spend1,2,3,4 变量设为不一定需要,我该怎么做?
【问题讨论】:
-
def __init__(self, date, spend1=0, spend2=0, spend3=0, spend4=0) -
@Gang 这就是精神 thx
-
@Gang 让它更健壮,有什么办法可以自动拉长那些spend_n 部分?像直到花费 1000 也是可能的。
-
d = {}, 将某事添加到 dict,Gathering(**d) -
**双星号指的是什么?我知道一个星号,但不知道其中两个