python - 在不带参数的函数中调用函数

标签 python function python-3.x

快速提问:

我想调用一个不带任何参数的主函数,在主函数中,我有几个其他函数确实有参数。我该怎么做呢?

以下是多个函数:

 # Takes the Portfolio dictionay, unpacks the multiple tuples, and calculates
 # the total price of the shares at time of purchase

def total_purchase_price(portfolio):
    totalprice = 0
    totalpurprice = 0
    for item in portfolio:
        purdate, purprice, numshares, sym, curprice = item
        totalprice += purprice * numshares
        totalpurprice = totalprice
    return totalpurprice

# Takes the Portfolio dictionay, unpacks the multiple tuples, and calculates
# the current total value of the shares

def total_value(portfolio):
    totalprice = 0
    totalvalueprice = 0
    for item in portfolio:
        purdate, purprice, numshares, sym, curprice = item
        totalprice += curprice * numshares
        totalvalueprice = totalprice
    return totalvalueprice   

# Takes the previous two functions, and subtracts them to get the total
# gain/lost of the shares

def total_gain(total_purchase_price, total_value, portfolio):
    gainlost = total_value - total_purchase_price
    return gainlost

例如。我现在拥有的(注意:我知道这行不通,只是为了我想要的,因为每个函数都会返回一个值):

def testQ1(total_purchase_price, total_value, total_gain, portfolio):
    print("Total Cost =", total_purchase_price)
    print("Current Value =", total_value)
    print("Total gain/lost =", total_gain)
    return

例如。我想要实现的目标:

def testQ2():
    total_purchase_price(portfolio)
    total_value(portfolio)
    total_gain(total_purchase_price, total_value, portfolio)
    print("Total Cost =", total_purchase_price)
    print("Current Value =", total_value)
    print("Total gain/lost =", total_gain)
    return   

我该怎么做?谢谢

最佳答案

使用类可能更容易:

class PortfolioParser:
    def __init__(self, portfolio):
        self.portfolio = portfolio
        self.price = self.total_purchase_price()
        self.value = self.total_value()
        self.gain = self.total_gain()

    def total_purchase_price(self):
        # Takes the Portfolio dictionay, unpacks the multiple tuples, and calculates
        # the total price of the shares at time of purchase
        totalprice = 0
        totalpurprice = 0
        for item in self.portfolio:
            purdate, purprice, numshares, sym, curprice = item
            totalprice += purprice * numshares
            totalpurprice = totalprice
        return totalpurprice      

    def total_value(self):
        # Takes the Portfolio dictionay, unpacks the multiple tuples, and calculates
        # the current total value of the shares
        totalprice = 0
        totalvalueprice = 0
        for item in self.portfolio:
            purdate, purprice, numshares, sym, curprice = item
            totalprice += curprice * numshares
            totalvalueprice = totalprice
        return totalvalueprice   

    def total_gain(self):
        # Takes the previous two functions, and subtracts them to get the total
        # gain/lost of the shares
        return self.value- self.price

    def testQ2(self):
        print("Total Cost = {0}\nCurrent Value = {1}\nTotal Gains = {2}".format(self.price, self.value, self.gain))

然后你会像这样使用它:

myPortfolio = # The portfolio to parse
parser = PortfolioParser(myPortfolio)
parser.testQ2()

关于python - 在不带参数的函数中调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35854048/

相关文章:

python - 正则表达式:正好匹配三行

python - Ubuntu 10.4 中 Vim 的 Pydiction 和 Python 的问题

javascript - 产生预期结果的函数中使用的方法背后的数学推理是什么?

javascript - d3.js 在 d3.js 特定函数中使用函数

python - Tkinter、asksaveasfile 和 unicode

python - 为什么在mac上使用PyQt5不能添加图标?

python - python中的全局变量引用

python - Teamcity pytest 插件和单元测试报告

c++ - 我可以在 C++ 中声明一个非成员函数 const 吗?

python - 如何为复杂的计算设置超时?