Python Forum

Full Version: Instances as attributes
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello everyone, I am trying to learn the concept of instances as attributes. Below is the code snippet
class User:
	def __init__(self,firstname,lastname):
		self.firstname = firstname
		self.lastname = lastname
	def describe_user(self):
		print(f'User 1 name is {self.firstname} {self.lastname}')

class Priviliges:
	def __init__(self, priviliges):
		self.priviliges = ['can add post','can delete post','can ban user']

	def show_priviliges(self):
		print(f'{self.firstname} {self.priviliges}')

class Admin(User):
	def __init__(self,firstname,lastname):
		super().__init__(firstname,lastname)
		self.priviliges = Admin()

myuser = Admin('Joe','Smith')

print (myuser.describe_user())
print (myuser.show_priviliges())
What is wrong with the above code? I keep on getting __init__ missing 2 required positional arguements error message. Thank you.
This line doesnt really make any sense.
Quote:
self.priviliges = Admin()
You are getting an error because Admin class takes two arguments and here you do not give any. But more than that, it doesnt make any sense to make that class an instance of itself. Another class, sure.

In addition to that myuser is an instance of Admin. show_priviliges() method is a method of the class Priviliges. Which is a completely different class. It looks like you are super confused about inheritance of classes. I would read our class inheritance tutorial.
metulburr beat me to it.