Python Forum
Unexpected Output using classes and inheritance
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unexpected Output using classes and inheritance
#1
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() 
Reply
#2
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
Reply
#3
Oops. I noticed that. Indentation is key. Thanks a lot. Works perfectly now.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Unexpected output Starter 2 474 Nov-22-2023, 12:08 AM
Last Post: Starter
  Unexpected Output - Python Dataframes: Filtering based on Overlapping Dates Xensor 5 702 Nov-15-2023, 06:54 PM
Last Post: deanhystad
  Unexpected output while using random.randint with def terickson2367 1 508 Oct-24-2023, 05:56 AM
Last Post: buran
  Unexpected output from df.loc when indexing by label idratherbecoding 6 1,185 Apr-19-2023, 12:11 AM
Last Post: deanhystad
  Inheritance vs Instantiation for Python classes mr_byte31 7 2,849 Oct-14-2021, 12:58 PM
Last Post: mr_byte31
  unexpected output asyrafcc99 0 1,496 Oct-24-2020, 02:40 PM
Last Post: asyrafcc99
  Unexpected output: symbols for derivative not being displayed saucerdesigner 0 2,042 Jun-22-2020, 10:06 PM
Last Post: saucerdesigner
  Unexpected output palladium 4 2,738 Jan-11-2020, 03:26 PM
Last Post: palladium
  Unexpected output: if statement CabbageMan 1 1,760 Sep-04-2019, 04:12 PM
Last Post: ThomasL
  Problems with inheritance with classes internetguy 3 2,587 Jul-04-2019, 11:59 AM
Last Post: metulburr

Forum Jump:

User Panel Messages

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