Something like:
class Processor(object):
def __init__(self):
self.byte0 = 0
processor_count = 4
processors = [Processor() for count in range(processor_count)]
for j in range(4):
print processors[j].byte0
or
class Processor(object):
def __init__(self):
self.byte0 = 0
def __str__(self):
return '%r, byte0 = %i' % (self, self.byte0)
processor_count = 4
processors = [Processor() for count in range(processor_count)]
for processor in processors:
print(processor)
pyTony
pyMod
6,331 posts since Apr 2010
Reputation Points: 879
Solved Threads: 990
Skill Endorsements: 27
If you want to know how to increment multiple sequences at once -
start_position = 0
cores = [1,2,3,4]
for core, position in zip(cores, xrange(start_position, len(cores))):
print core, position
I'm not sure what you mean by "allows the processor to be incremented to the next one".
class Processor(object):
def __init__(self, core_number):
self.core_number = core_number
def __str__(self):
return repr('Ohai, I have #{core_number} cores'.format(core_number=self.core_number))
processors = []
for core_number in xrange(4):
processors.append(Processor(core_number))
for processor in processors:
print processor
print 'I am of type {object_type}'.format(object_type=type(processor))
Perhaps that's what you mean? A dict, of course, would be better suited to all examples of this.
Enalicho
Junior Poster in Training
62 posts since Aug 2011
Reputation Points: 28
Solved Threads: 13
Skill Endorsements: 0
# a test class we can instantiate
class Instance:
def __init__(self, number):
self.number = number
Why not give it a sensible name, because you'll probably come back to it later, like "Processor" or "Core".
processors = [Instance(n) for n in range(6)]
Funny name, for a list of instances of Instance ;)
for f in range(0,3):
print processors[f].number
Well, you're missing out two processors/cores. What did they ever do to you? :P
Seriously though, why not use the lovely syntax of -
for processor in processors:
print processor.number
Enalicho
Junior Poster in Training
62 posts since Aug 2011
Reputation Points: 28
Solved Threads: 13
Skill Endorsements: 0
Question Answered as of 1 Year Ago by
Enalicho
and
pyTony