Python Forum
Polymorphism not working with a call to a abstract method - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Polymorphism not working with a call to a abstract method (/thread-22207.html)



Polymorphism not working with a call to a abstract method - colt - Nov-03-2019

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:

class LDA (Instruction):
	def process (self,registers,ram,rom):
        #do lots of stuff 
        writeAccumulator (registers,ram,rom) 


@abstractmethod
	def writeAccumulator (self,registers,ram,rom):
		pass
class LDAImediate (LDA):

	def writeAccumulator (self,registers,ram,rom):
		registers.accumulator = rom.data_bytes[registers.pc+1]
		print ("registers.accumulator: ", registers.accumulator)
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)
However, when trying to run it I receive:

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.


RE: Polymorphism not working with a call to a abstract method - ichabod801 - Nov-04-2019

You need to call it as self.writeAccumulator(registers, ram, rom). Otherwise Python assumes the containing namespace of 'writeAccumulator' is either local or global.


RE: Polymorphism not working with a call to a abstract method - MckJohan - Nov-04-2019

can you explain a little bit more on the problem?

from LDA class, the Process function is calling writeAccumulator which doesn't have implementation. do you want python to search the implementation to all subclass and used it? where are you calling at? it will be helpful where did you call. it may solved using super() or adapter pattern.


RE: Polymorphism not working with a call to a abstract method - colt - Nov-04-2019

(Nov-04-2019, 01:24 AM)MckJohan Wrote: can you explain a little bit more on the problem?
do you want python to search the implementation to all subclass and used it?[/quote]
Yes.

Quote:where are you calling at? it will be helpful where did you call. it may solved using super() or adapter pattern.

Well, you mentioned the place on your message:
Quote:from LDA class, the Process function is calling writeAccumulator which doesn't have implementation.

writeAccumulator is been called on the process method. Anyway, it was merely the lack of self that ichabod801 mentioned, that prevented it to work as I expected.