亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將一個函數的結果存儲到一個類中?

如何將一個函數的結果存儲到一個類中?

心有法竹 2022-11-01 14:34:15
所以我創建了兩個類。第一個(myModel)有一個函數,可以用“predictFinalIncome”函數計算收入。我出于 SO 的目的對其進行了簡化。class myModel:  """  The earning growth model for individuals in the utopia   """  def __init__(self, bias) :    """    :param bias: we will use this potential bias to explore different scenarios to the functions of gender and ethnicity    :param b_0: the intercept of the model.\     :param b_age: age at world creation    :param b_education: similar.     :param b_gender: similar    :param b_marital: marital status    :param b_ethnic: similar    :param b_industry: similar    :param b_income: similar.    """    self.bias = bias # bias is a dictionary with info to set bias on the gender function and the ethnic function  def predictFinalIncome( self, n, person ):     for i in range(n):      n_income = n_income* i    return n_income所以這個類接受一個“Person”字典,比如:utopModel = myModel( { "gender": False, "ethnic": False } ) months = 12plato = { "age": 58, "education": 20, "gender": 1, "marital": 0, "ethnic": 2, "industry": 7, "income": 100000 }utopModel.predictFinalIncome(months,plato)所以我的目標是創建一個類(Person),它可以在每次調用該函數時存儲給定 Person 的 predictFinalIncome(),從而刪除前一個。這樣我就可以跟蹤一個人并在我調用該函數時存儲他們的預測收入。我想將其作為收入存儲在 Person 中。class Person:  """  The attributes of a Person to build a person up, having their information in one place as it changes.  """  def __init__(self, bias) :    """    :param age: person's age    :param education: person's years of education     :param gender: male or female    :param marital: marital status    :param ethnic: ethnicity    :param industry: what sector of work    :param income: salary    """  def age00(self, age):    return age  def age(self, age):    return   def income00(self, income):    return income  def income(self, n, income):    return   def __getitem__(self, item):    return self.__dict__[item]
查看完整描述

2 回答

?
交互式愛情

TA貢獻1712條經驗 獲得超3個贊

我認為這里有兩種解決方案:

  1. predictFinalIncome在里面寫Person。當您調用predictFinalIncome方法時,您會將收入值保存為Person類的屬性

  2. 您將Person實例作為predictFinalIncome方法的參數傳遞。在計算收入后,您可以使用該實例進行儲蓄。見下文

def predictFinalIncome( self, n, specificPerson: Person ): 

    for i in range(n):

      n_income = n_income* i


    # new lines

    specificPerson.income += n_income #income updated

    specificPerson.age = ((specificPerson.age*12) + n)/12 # age updated

當你predictFinalIncome在外面打電話時:


utopModel = myModel( { "gender": False, "ethnic": False } ) 

months = 12

specificPerson = Person(..something here..)

utopModel.predictFinalIncome(months,specificPerson)

現在,當您調用方法時,您的specificPerson實例會自動更新收入predictFinalIncome


查看完整回答
反對 回復 2022-11-01
?
holdtom

TA貢獻1805條經驗 獲得超10個贊

所以 NimaNr 的解決方案(在您的評論中)可能是最簡單的解決方案。

如果您將 predictFinalIncome() 保留在 myModel 類中,您將需要為您的 Person 的收入變量創建一個setter函數。它看起來像這樣:

def setIncome(self, x):
    self.Income = x

你想要在你的predictFinalIncome方法中做的是用你得到的 n_income 值調用我們剛剛在上面創建的設置器。


查看完整回答
反對 回復 2022-11-01
  • 2 回答
  • 0 關注
  • 108 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號