Nov-03-2019, 11:54 PM
Hello. I was creating two classes that both implemented an abstract method of their parent class.
However, as I went to implement them, I realized that both did about the same thing, at least in the first 2/3 of the method.
So I had this idea. Will expand the class hierarchy, creating two new classes that will implement a new abstract method that will do the last 1/3 that was going to be different in the original classes
So this is how my code is currently:
However, when trying to run it I receive:
My idea was that the call to the correct "writeAccumulator" would happen, since the code only creates instances of "LDAImediate" and "LDAbsolute" and never of "LDA", where "writeAccumulator" do not exist.
So, is there a way to implement it as I want? Thank for the time.
However, as I went to implement them, I realized that both did about the same thing, at least in the first 2/3 of the method.
So I had this idea. Will expand the class hierarchy, creating two new classes that will implement a new abstract method that will do the last 1/3 that was going to be different in the original classes
So this is how my code is currently:
1 2 3 4 |
class LDA (Instruction): def process ( self ,registers,ram,rom): #do lots of stuff writeAccumulator (registers,ram,rom) |
1 2 3 |
@abstractmethod def writeAccumulator ( self ,registers,ram,rom): pass |
1 2 3 4 5 |
class LDAImediate (LDA): def writeAccumulator ( self ,registers,ram,rom): registers.accumulator = rom.data_bytes[registers.pc + 1 ] print ( "registers.accumulator: " , registers.accumulator) |
1 2 3 4 5 6 7 8 9 10 |
class LDAbsolute (LDA): def writeAccumulator ( self ,registers,ram,rom): address1 = rom.data_bytes[registers.pc + 1 ] address2 = rom.data_bytes[registers.pc + 2 ] print ( "address1: " , address1, "address2: " , address2) address2 = (address2 << 8 ) + address1 print ( "final address: " , address2) registers.accumulator = rom.data_bytes[address2] print ( "registers.accumulator: " , registers.accumulator) |
Quote:File "/home/leopoldo/instruction.py", line 49, in process
writeAccumulator (registers,ram,rom)
NameError: global name 'writeAccumulator' is not defined
My idea was that the call to the correct "writeAccumulator" would happen, since the code only creates instances of "LDAImediate" and "LDAbsolute" and never of "LDA", where "writeAccumulator" do not exist.
So, is there a way to implement it as I want? Thank for the time.