Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Help] inhe
#1
Hi All ,

I am new to Python and i have written the below code where a child class inherits from the parent class
but when i call any method of the parent class from a instance of the child class i get the error as mentioned below :-

My Code :-

import random

class Coin:
    
    def __init__(self, rare=False,clean=True,heads= True,**kwargs):

        for key,value in kwargs.items():
            setattr(self,key,value)
            print("Hi how are you ?")
        
        self.is_rare = rare
        self.is_clean=clean
        self.heads=heads
        if self.is_rare:
            self.value=self.original_value*1.25 
        else:
            self.value=self.original_value

        if self.is_clean:
            self.color=self.clean_color
        else:
            self.color=self.rusty_color

        def rust(self):
            self.color=self.rusty_color

        def clean(self):
            self.color=self.clean_color

        #def __del__(self):
         #print("Coint Spent!")
        

        def flip(self):            
            heads_options =[True, False]
            choice = random.choice
            (heads_options)
            self.heads= choice
        
class Pound(Coin):
    def __init__(self):
        data = {
            "original_value": 1.00,
            "clean_color": "gold",
            "rusty_color":"greenish",
            "num_edges":1,
            "diameter":22.5,
            "thickness":3.15,
            "mass":9.5
            }
        
        super().__init__(**data)
*************************

When i run like this i get the message as below where i have defind all the methods in the parent class

one_pound=Pound()
?
Error:
>>> one_pound.color 'gold' >>> one_pound.rust() Traceback (most recent call last): File "<pyshell#173>", line 1, in <module> one_pound.rust() AttributeError: 'Pound' object has no attribute 'rust'
Reply
#2
you need to allign lines 24-38 with the __init__
at the moment they are defined and available only in the __init__
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Hi ,

I am not clear on your solution , Can you please correct it on the code once .


Thanks,
Mike
Reply
#4
class Coin:
     
    def __init__(self, rare=False,clean=True,heads= True,**kwargs):
 
        for key,value in kwargs.items():
            setattr(self,key,value)
            print("Hi how are you ?")
         
        self.is_rare = rare
        self.is_clean=clean
        self.heads=heads
        if self.is_rare:
            self.value=self.original_value*1.25 
        else:
            self.value=self.original_value
 
        if self.is_clean:
            self.color=self.clean_color
        else:
            self.color=self.rusty_color
 
    def rust(self):
        self.color=self.rusty_color

    def clean(self):
        self.color=self.clean_color

    #def __del__(self):
        #print("Coint Spent!")
        

    def flip(self):            
        heads_options =[True, False]
        choice = random.choice
        (heads_options)
        self.heads= choice
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
Yes Buran is correct.

To be more clear, the issue is that your extra indentation has defined the methods inside the the __init__ method, so they would have only been able to be called during instantiation. By unindenting, the methods move back to being instance methods.

This was not an issue with inheritance. You would have seen this exception on an instance of Coin as well.
Reply


Forum Jump:

User Panel Messages

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