Python Forum
Unexpected Output using classes and inheritance - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Unexpected Output using classes and inheritance (/thread-19555.html)



Unexpected Output using classes and inheritance - langley - Jul-04-2019

I am a beginner at python and in the code below I get an error saying attribute was not defined or saying a list is not callable if I make a few changes. Please help me through this.
class User():
	"""Storing information about any user"""
	def __init__(self,first_name,last_name,age,location,gender):
		"""Holds the information"""
		self.first_name=first_name
		self.last_name=last_name
		self.age=age
		self.location=location 
	def describe_user(self):
		print(self.first_name.title() + " " + self.last_name.title() + " " +"aged " + str(self.age) +" " + "lives in " + self.location.title()) 
class Admin(User): 
	def __init__(self,first_name,last_name,age,location,gender):
		super(). __init__(first_name,last_name,age,location,gender)
		self.privileges=[]
		def privileges(self):
			self.privileges=[]
			self.privileges.append("can ban post")
			self.privileges.append("can revoke access")
			self.privileges.append("can ban user")
			print("The privileges of an admin are:")
			for privilege in self.privileges:
				print("\n\t" + privilege)
new_user=Admin('abc','bac', 48, 'vienna' , 'male')
new_user.describe_user()
new_user.privileges() 



RE: Unexpected Output using classes and inheritance - scidam - Jul-04-2019

The priveleges function is defined inside the __init__ function. So, you are trying to invoke a list: new_user.privileges is a list (see line No. 14). I make some changes in your code, I suppose they help:

class User():
    """Storing information about any user"""

    def __init__(self, first_name, last_name, age, location, gender):
        """Holds the information"""

        self.first_name = first_name
        self.last_name = last_name
        self.age = age
        self.location = location 
    
    def __str__(self):
        return " ".join([self.first_name.title(), self.last_name.title(),
                         "aged", str(self.age), "lives in", self.location.title()])
        

class Admin(User): 
    def __init__(self,first_name, last_name, age, location, gender):
        super().__init__(first_name,last_name,age,location,gender)
        self.privileges = []

    def set_privileges(self):
        self.privileges = []
        self.privileges.append("can ban post")
        self.privileges.append("can revoke access")
        self.privileges.append("can ban user")
        print("The privileges of an admin are:")
        for privilege in self.privileges:
            print("\n\t" + privilege)

new_user = Admin('abc','bac', 48, 'vienna' , 'male')
print(new_user)
new_user.set_privileges()
new_user.privileges



RE: Unexpected Output using classes and inheritance - langley - Jul-04-2019

Oops. I noticed that. Indentation is key. Thanks a lot. Works perfectly now.