Guest User

Untitled

a guest
Jul 22nd, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. class Person:
  2.  
  3. def __new__(cls, first, last):
  4. print("Calling __new__() method of class {}".format(cls))
  5. return object.__new__(cls, first, last)
  6.  
  7. def __init__(self, first, last):
  8. """Constructor of Person working instance
  9. (attribute initialization)"""
  10. print("Calling __init__()")
  11. self.first = first
  12. self.last = last
  13. self.age = 23
  14. self.residency = "Lyon"
  15.  
  16. def __repr__(self):
  17. return "Person : {} {} aged {} years living in {}".format(self.first, self.last, self.age, self.residency)
  18.  
  19. person = Person("Doe", "John")
  20. print(person)
  21.  
  22. Calling __new__() method of class <class '__main__.Person'>
  23. Traceback (most recent call last):
  24. File "test.py", line 20, in <module>
  25. person = Person("Doe", "John")
  26. File "test.py", line 6, in __new__
  27. return object.__new__(cls, first, last)
  28. TypeError: object() takes no parameters
Add Comment
Please, Sign In to add comment