abdurrafay8801
abdurrafay8801

Reputation: 61

How to treat a class, parameters

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):

Upvotes: 0

Views: 48

Answers (1)

Jacques Gaudin
Jacques Gaudin

Reputation: 16958

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

Upvotes: 4

Related Questions