动态创建实例时如何调用函数?

2024-05-15 18:47:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我是一名Java新手,正在过渡到Python,我一直在尝试在公司和投资者之间开发一种简单化的类似市场的交互。基本上,有100家公司和100名投资者在初始化时以随机值动态创建

class Company:
    def __init__(self):
        """ Constructor for the Company object """
        self.id: str = uuid.uuid4().hex
        self.shares_num = random.randint(500, 1000)
        self.share_price = Decimal(random.randint(10, 100))

class Investor:
    def __init__(self):
        """ Constructor for the Investor object """
        self.id_inv: str = uuid.uuid1().hex
        self.budget: Decimal = random.randint(50_000, 100_000)
        self.acq_shares = 0

    def sell_shares(self):
        trade = self.budget - Company.get_share_price
        Company.get_shares_num -= 1
        self.acq_shares += 1
        self.set_budget = trade
        return self

函数sell_shares()执行实际销售,即在投资者的资金和公司的股票之间进行交易

然后,这些公司和投资者被放入一个名单,名单上有他们的ID和一份公司股票数据和投资者预算的dict

# dynamic instances of Companies and Investors
comp = [Company() for _ in range(10)]
companies = {c.id: c for c in comp}

inv = [Investor() for _ in range(10)]
investors = {i.id_inv: i for i in inv}

问题是,当我调用sell_shares()方法Investors.sell_shares时,公司或股票的实例都不会受到影响。除了它不起作用之外,我真的看不出有什么问题。 有没有关于我如何尝试解决这个问题的建议

万分感谢:)

注意:我正在使用标准库的Python 3.8.2


Tags: inselfidfor投资者def公司random
1条回答
网友
1楼 · 发布于 2024-05-15 18:47:12

您最大的痛苦可能是在类方法中使用可变全局变量。请不要那样做。每次你这样做,总会有一只小猫死

通过让trade()接受公司的关键字参数,然后使用self(指特定的投资者)和关键字参数(指公司的特定实例)进行操作,可以很容易地解决这个问题。不要试图一次访问所有的投资者和公司,因为你的方法只取决于其中一个

import uuid
import random
from decimal import Decimal

class Company:
    def __init__(self):
        """ Constructor for the Company object """
        self.id: str = uuid.uuid4().hex
        self.shares_num = random.randint(500, 1000)
        self.share_price = Decimal(random.randint(10, 100))

class Investor:
    def __init__(self):
        """ Constructor for the Investor object """
        self.id: str = uuid.uuid1().hex
        self.budget: Decimal = random.randint(50000, 100000)
        self.acq_shares = 0

    def trade(self, company):
        trading = self.budget - company.share_price
        company.share_price -= 1
        self.acq_shares += 1

        # this equation is wrong, but I'm not sure entirely what you want to do here
        # this is equivalent to -company.share_price
        company.share_price = trading - self.budget

        self.budget = trading
        return self.id, self.budget 

注意,这也消除了创建get_share_price()get_budget()方法的任何需要

如果,不知何故,我误解了您的设置,您的交易功能确实同时依赖所有投资者和所有公司,那么不要将其作为一种分类方法。让它成为一个常规函数,在类之外,它接受两个参数,比如sodef trade(investors, companies):,因为在这种情况下,它不必处理investor的单个类实例

您可以这样测试您的类:

# Generate companies and investors
comp = [Company() for _ in range(10)]
companies = {c.id: c for c in comp} # <  not sure what this is used for

inv = [Investor() for _ in range(10)]
investors = {i.id: i for i in inv} # <  not sure what this is used for

# Select an investor and a company
some_investor = inv[random.randint(0,9)]
some_company = comp[random.randint(0,9)]

# Make a trade
some_investor.trade(some_company)

print(some_company.share_price) # prints a negative number

请注意,您当前的股价公式对我来说似乎非常错误,因为它相当于self.budget - company.share_price - self.budget

希望这有帮助

相关问题 更多 >