Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use an attribute from one class in an another class?

I have 2 files: user.py (where I am doing all the printing) and follower.py (just to give follower data). I want to print the follower name via user.py but getting attribute error there.

  • user.py

    from follower import Follower
    
    class User:
    
      def __init__(self, user_email, user_name):
    
        self.email=user_email
        self.name=user_name
    
      def print_details(self):
    
         print(f" user details : {user1.name}\n{user1.email}")
    user1=User("ABC@gmail.com", "ABCDEF")
    
    
    user1.print_details()
    
    print(Follower.name)
    
    Follower.message_from_follower()
    
  • follower.py

    MEDevel.com: Open-source for Healthcare and Education

    Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

    Visit Medevel

    class Follower:
    
     def __init__(self,follower_name, job, loc):
    
        self.name = follower_name
        self.job = job
        self.location = loc
    
     def message_from_follower():
    
         return "Good day"
    
    
    f_details= Follower(" XYZ", "teacher", "AUS")
    

>Solution :

I am now sure you are trying to do here. If you want to print name of the follower ("XYZ") then you probably want to import the instance f_details instead of the class Follower.

But I wouldn’t recommend to import instance. Instantiate the class in user.py instead.

f_details= Follower(" XYZ", "teacher", "AUS")
print(f_details.name)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading