猿问

如何对待一个类、参数

我想知道我可以在一个类中放入多少个参数,有没有限制或者是否有更漂亮的放置属性的方法。看看代码并指导我

class residential():
    def __init__(self,type,capacity,location,parking,mosque,waterbackup,cctvsecure):


哔哔one
浏览 189回答 1
1回答

慕村9548890

Python 中任何函数的最大参数数曾经是 255。自 Python 3.7 起,此限制已被删除。Python 3.7 中的另一个新颖之处是新的模块数据类,它简化了具有多个参数的类的声明。例如这段代码:@dataclassclass InventoryItem:    '''Class for keeping track of an item in inventory.'''    name: str    unit_price: float    quantity_on_hand: int = 0    def total_cost(self) -> float:        return self.unit_price * self.quantity_on_hand将添加,除其他外,一个__init__()看起来像:def __init__(self, name: str, unit_price: float, quantity_on_hand: int=0):    self.name = name    self.unit_price = unit_price    self.quantity_on_hand = quantity_on_hand
随时随地看视频慕课网APP

相关分类

Python
我要回答