for param in Parameters:
#
Above will not work because Parameters
is class and it is not iterable.
class Parameters(Enum):
PARAM1 = (0, "some msg 1")
PARAM2 = (1, "some msg 2")
PARAM1
and PARAM2
are class variables in above case.
We have to do something like this:
class Enum(object):
def __init__(self, value):
self.value = value[0]
self.msg = value[1]
class Parameters(Enum):
def _init__(self, value):
super.__init__(value)
PARAM1 = (0, "some msg 1")
PARAM2 = (1, "some msg 2")
for param in [PARAM1, PARAM2]:
print Parameters(param)
[Edit 1]:
Create number of objects by for loop with range function
code:
for i in range(100):
param = (i, "some mesg %s"%i)
print Parameters(param)
[Edit 2]:
Get values from the User by raw_input
function and type conversion from the string to integer
Demo:
>>> no = raw_input("Enter Number of objects you want to create(Give only number): ")
Enter Number of objects you want to create(Give only number): 33
>>> no
'33'
>>> type(no)
<type 'str'>
>>> no_int = int(no)
>>> type(no_int)
<type 'int'>
Note:
use raw_input()
in Python 2.x
use input()
in Python 3.x
[Edit 3]: Hardcode values by Instance variables method.
Define class in hardcoded_values.py
file
class Hardcodes():
def __init__(self,):
self.para1 = (1, "some msg 1")
self.para2 = (2, "some msg 2")
self.para3 = (3, "some msg 3")
self.para4 = (4, "some msg 4")
Import Hardcodes
class in test.py
file
#- Create Object(instance)
from hardcoded_values import Hardcodes
obj = Hardcodes()
hardcode_values = obj.__dict__
for i in hardcode_values.iteritems():
print i
Output:
$ python test.py
('para3', (3, 'some msg 3'))
('para2', (2, 'some msg 2'))
('para1', (1, 'some msg 1'))
('para4', (4, 'some msg 4'))