Python Forum
Polymorphism not working with a call to a abstract method
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Polymorphism not working with a call to a abstract method
#1
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.
Reply
#2
You need to call it as self.writeAccumulator(registers, ram, rom). Otherwise Python assumes the containing namespace of 'writeAccumulator' is either local or global.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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.
Reply
#4
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  method call help sollarriiii 6 1,078 Feb-21-2023, 03:19 AM
Last Post: noisefloor
  Custom method to handle exceptions not working as expected gradlon93 3 944 Dec-22-2022, 07:12 PM
Last Post: deanhystad
  Behavior of Abstract Base Class dgrunwal 4 2,135 Oct-15-2020, 07:19 PM
Last Post: Gribouillis
  Abstract classes SephMon 8 3,043 Aug-30-2020, 05:03 AM
Last Post: Gribouillis
  remove elements method not working spalisetty06 4 2,409 Aug-13-2020, 01:17 PM
Last Post: deanhystad
  python os.popen is not working for wait method elenaflorence87 0 1,968 Jul-22-2020, 12:56 PM
Last Post: elenaflorence87
  how to get around recursive method call Skaperen 10 4,191 Jul-01-2020, 10:09 PM
Last Post: Skaperen
  How to convert TransactSQL into Abstract Syntax Tree (AST) using Python? bajrangbs 2 2,280 Jan-30-2020, 03:46 PM
Last Post: micseydel
  How to call COM-method using comtypes jespersahner 0 2,386 Nov-15-2019, 12:54 PM
Last Post: jespersahner
  How to Call a method of class having no argument dataplumber 7 6,319 Oct-31-2019, 01:52 PM
Last Post: dataplumber

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020