I've been trying to work myself through an error:
I have a child class with an instance as an attribute. The attribute self.admin_privileges
should take Python to the Privileges
class and then print out the list attributed to self.privileges
.
However, when a make the call fortesque.admin_privileges.show_privileges()
I get a type error:
missing 1 required positional argument: 'privileges'
Here is an image of my code:
In your code example you have a class definition that looks something like this:
class Privleges():
def __init__(self, privleges):
self.privleges = ['can do something']
class Admin():
def __init__(self):
self.privleges = Privleges()
The problem is that the "Privileges" class expects to be instantiated with an argument "privileges" as defined by your init function.
For your admin class you did not require any such param when instantiating new Admins as shown by the init function that only takes "self" and nothing additional.
To fix the issue you probably want to remove "privileges" from the init function for the "Privileges" class, since it gets set by the init function.
class Privileges():
def __init__(self):
self.privileges = ['can do something']
Another option however would be to create a default list of privileges while also allowing the Admin class the ability to overwrite the value. That would look something like this.
class Privileges():
def __init__(self, privileges=['can do the default action']):
self.privileges = privileges
class Admin():
def __init__(self):
self.privileges = Privileges(['can do Admin Stuff'])
class NotAnAdmin():
def __init__(self):
self.privileges = Privileges() # gets the default