Advertisement
FutureDreams

Untitled

Feb 7th, 2020
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.99 KB | None | 0 0
  1. class User:
  2.  """
  3.  Basic User class, keeps name and age which is common for all users
  4.  """
  5.  name = "User"
  6.  age = 0
  7.  
  8. class Privilege:
  9.  """
  10.  To represent privilege object
  11.  Only holds type and name of the object
  12.  """
  13.  def __init__(self, p_type, p_name):
  14.   self.p_type = p_type
  15.   self.p_name = p_name
  16.  def __repr__(self):
  17.   return "Type: {}, name: {}".format(self.p_type, self.p_name)
  18.  
  19. class Admin(User):
  20.  """
  21.  Admin class inherits from basic User class taking the name and age fields from its parent
  22.  In case we don't provide those two buddies, default values will be used (User and 0)
  23.  """
  24.  def __init__(self, privileges, name=None, age=None):
  25.   if name:
  26.    self.name = name
  27.   if age:
  28.    self.age = age
  29.   self.privileges = privileges
  30.  
  31.  def show_privileges(self):
  32.   print(self.privileges)
  33.  
  34.  def show_info(self):
  35.   print("{}: age={}".format(self.name, self.age))
  36.  
  37. admin = Admin([Privilege(0, "write"), Privilege(1, "read")])
  38.  
  39. admin.show_privileges()
  40. admin.show_info()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement