954,515 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Multiple Class Objects that can be "incremented"

Dim Processors(4) As ProcessorType

For j = 1 To 4 Step 1
       Put #filenum, WritePos, Processors(j).byte0
       WritePos = WritePos + 1


I'm in the process of translating this VBA code to python, and essentially what it does is creates 4 Processor Objects but as you can see from the for loop it allows the processor to be incremented to the next one so that it's byte0 from each processor is written.

How would you do this in python, any advice?

kayoh
Newbie Poster
9 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
 

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
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

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
 

Thank you!

I see we came up with a similar solution, it's essentially a list of object instances right?

The Following is what I came up with.

# a test class we can instantiate
class Instance:
    def __init__(self, number):
        self.number = number
        
processors = [Instance(n) for n in range(6)]
 
for f in range(0,3):
    print processors[f].number

print type(processors)
print type(f)
print type(processors[0])
kayoh
Newbie Poster
9 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
 

Enalicho, what I meant was a way to increment to the next class object in a for loop, The solutions we came up with work by putting each object instance in a list and then calling the list and incrementing the list's index to get to each item

kayoh
Newbie Poster
9 posts since Aug 2011
Reputation Points: 10
Solved Threads: 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
 

Haha, I totally get you, I have all the Processors listed out and named in my actual code but this is just an example I used to implement what I was trying to accomplish.

My actual program is generating a file and depending on which unit there are a different number of processors, so the biggest unit has 3 processors and each one has 4 bytes of data that need to be packed into a file along with file names and other things. The actual size of each file varies from unit to unit. So I'm using a for loop that can go from each object and put in the bits and addresses and what not.

kayoh
Newbie Poster
9 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: