Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Instances as attributes
#1
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.
Reply
#2
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.
Recommended Tutorials:
Reply
#3
metulburr beat me to it.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question [solved] Classes, assign an attributes to a class not to instances.. SpongeB0B 4 891 May-20-2023, 04:08 PM
Last Post: SpongeB0B
  Instances sharing attributes midarq 4 2,445 Sep-20-2019, 11:13 AM
Last Post: midarq
  Class Instances overriding class members and attributes. Leaf 7 6,860 Nov-29-2017, 06:08 AM
Last Post: Leaf

Forum Jump:

User Panel Messages

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