Intitalize values for a class

Ganesh Pal ganesh1pal at gmail.com
Fri Nov 23 08:41:45 EST 2018


Hello team,

I am a python 2.7 user on Linux. I will need feedback on the below program
as I'm  new to oops .

#!/usr/bin/python


class System(object):

  '''Doc - Inside Class '''

  def __init__(self, params=None):

       if params is None:

          self.params = {'id': '1',

                          'name': 's-1'}

          print self.params

       if type(params) is dict and params.get('id') == '0':

             raise ValueError('ERROR: id 0 is reserved !! ')

           #print self.params

       else:

            self.params = params

            print self.params

# Test all conditions

#case 0 - Default should create {'id': '1','name': 's-1'}
#s0 = System()

#Case 1 (id has value '0')
#test1_params = {'id': '0', 'name': 's-0'}
#s1 = System(params=test1_params)


#Case 2 (id has some other values)
#test2_params = {'id': '10', 'name': 's-10'}
#s2 = System(params=test2_params)


Question:

I have to initialize the values the below class such that

 1.  Intitalize  default values if nothing is supplied by the username  i.e
self.params = {'id': '1', 'name': 's-1'}

2. I need to raise an Exception if the value for the key params[id] is '0'.

3. It should work if  params[I'd] has values other than (1) and (2)

Regards,
Ganesh



More information about the Python-list mailing list