Bottom Page

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.
Quote
#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.
colt likes this post
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#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.
colt likes this post
Quote
#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.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  How to convert TransactSQL into Abstract Syntax Tree (AST) using Python? bajrangbs 2 118 Jan-30-2020, 03:46 PM
Last Post: micseydel
  How to call COM-method using comtypes jespersahner 0 112 Nov-15-2019, 12:54 PM
Last Post: jespersahner
  How to Call a method of class having no argument dataplumber 7 514 Oct-31-2019, 01:52 PM
Last Post: dataplumber
  Call method from another method within a class anteboy65 3 257 Sep-11-2019, 08:40 PM
Last Post: Larz60+
  What is the use of call method and when to use it? everyday1 1 331 Jul-14-2019, 01:02 PM
Last Post: ichabod801
  I'm trying to figure out whether this is a method or function call 357mag 2 409 Jul-04-2019, 01:43 AM
Last Post: ichabod801
  How to call a method in a module using code KingPieter 4 517 Jan-15-2019, 09:13 PM
Last Post: KingPieter
  Why is subprocess.call command not working? zBernie 5 3,113 Nov-19-2018, 11:11 PM
Last Post: snippsat
  Polymorphism in Python Question Oliver 6 1,338 Dec-13-2017, 08:26 PM
Last Post: Oliver
  Write method not working Raudert 8 2,391 Jul-27-2017, 07:30 PM
Last Post: sparkz_alot

Forum Jump:


Users browsing this thread: 1 Guest(s)