0

I must write a class Person with 3 attributes (Name,Surname,Age) in all his instances. How can i set it that it cant be possible to add more attributes?

class Person:
   def __init__(self,name,surname,age):
        self.name=name
        self.surname=surname
        self.age=age
Kisegi
  • 23
  • 4
  • Does your assignment actually require you to block attempts to set other attributes, or is your assignment just telling you not to set any other attributes? – user2357112 Dec 13 '20 at 11:57

1 Answers1

1

use __slots__:

class Person:
   __slots__ = ("name", "surname", "age")
   def __init__(self,name,surname,age):
        self.name=name
        self.surname=surname
        self.age=age

Read more here: Usage of __slots__?

Itay Dumay
  • 139
  • 10