If you’ve defined a new method inside your class definition remember that it’s bound to that class (it’s not a regular function and cannot be immediately accessed in the global namespace) and is an attribute of any instances you create from it and can be accessed like so: instance.method(). You’re treating it like a function which it is not. You’d have to then write your own function which calls the .__len__ method of an instance passed to it to make it work in the way you did.
Calling that method with the built-in, len() is the accepted way and you’d probably raise a few eyebrows if you did it any other way.
In the exercise if i use this method the output remains the same and no error is given so i am having a hard time understanding the use of these methods as just adding the attribute seems easier than creating a dunder method
So trying to use the iter dunder method and not really sure how it works and what i can do with it in this context, nothing was givin in the example other than syntax and it was not used in the problem. I added it to my code but not sure what to do with it or the syntax on how to use it.
class LawFirm:
def __init__(self, practice, lawyers):
self.practice = practice
self.lawyers = lawyers
def __len__(self):
return len(self.lawyers)
def __contains__(self,lawyer):
return lawyer in self.lawyers
def __iter__(self):
return iter(self.lawyers)
d_and_p = LawFirm("Injury", ["Donelli", "Paderewski"])
print (len(d_and_p))
print ("Donelli" in d_and_p)
You’ve created each user as a new object so passing an instance of User to print will give you a representation of the object stored instead of the exact attribute you’re looking for that probably looks something like the following-
Out: <__main__.User object at 0x7ffffffffff>
Not too helpful when you want to find which users have editing permissions so using the .username attribute is the way to go.
What most of the built-ins have, and what you can implement yourself, are dunder methods to provide cleaner string representations when the object is printed. The __str__ method in particular could be used here to return the user.username and then print would do what you want- https://docs.python.org/3/reference/datamodel.html#object.str
__repr__ is a similar method but it is designed more for debugging so in this example __str__ is more appropriate (repr should return something more like User("diana") which is a valid Python expression that could recreate the given object. https://docs.python.org/3/reference/datamodel.html#object.repr