These special methods have a naming convention, where the name starts with two underscores, followed by an identifier and ends with another pair of underscores.Įssentially, each built-in function or operator has a special method corresponding to it. Python does all this using special methods. But you can configure the + operator in such a way that it appends a new item to the cart. Again, someone new to Python would think of implementing a method called append_to_cart() that takes an item and appends it to the cart list. In another case, we might want to append something to the cart. But you can configure the built-in len() in such a way that it returns the length of the cart list when given our object. Someone new to Python might decide to implement a method called get_cart_len() in their class to do this. ![]() In such a case, it is quite natural to want to obtain the length of the cart list. ![]() Note: If you need a refresher on OOP in Python, check out this tutorial on Real Python: Object-Oriented Programming (OOP) in Python 3
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |