Home > Net >  How to use an attribute from one class in an another class?
How to use an attribute from one class in an another class?

Time:07-18

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("[email protected]", "ABCDEF")
    
    
    user1.print_details()
    
    print(Follower.name)
    
    Follower.message_from_follower()
    
  • follower.py

    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")
    

CodePudding user response:

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)
  • Related