0

I want to know that how many parameters I can put in a class, is there any limit or if there a more handsome way for putting attributes.have a look at the code and guide me

class residential():
    def __init__(self,type,capacity,location,parking,mosque,waterbackup,cctvsecure):
  • 1
    The duplicate target doesn't seem to match. It would be worth pointing that the 255 arguments limit was removed in Python 3.7 and that dataclasses allows to omit the `__init__` function of a class with multiple arguments. I think the question is valid. – Jacques Gaudin Mar 25 '19 at 18:59
  • 1
    It's unclear what you are asking. Do you mean how many parameters can a function or method be defined as accepting, or do you mean how many attributes can a class have? – martineau Mar 25 '19 at 19:21

1 Answers1

4

The maximum number of arguments for any function in Python used to be 255. Since Python 3.7, this limit has been removed.

Another novelty in Python 3.7 is the new module dataclass which simplifies the declaration of a class with multiple arguments.

For example this code:

@dataclass
class 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

Will add, among other things, a __init__() that looks like:

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
Jacques Gaudin
  • 15,779
  • 10
  • 54
  • 75