From the course: Python for JavaScript Developers
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Super powers with the data model - Python Tutorial
From the course: Python for JavaScript Developers
Super powers with the data model
- In Python, you'll often see methods whose names begin and end with two underscores. These are often referred to as special methods, magic methods or Dunder methods, which is short for a double underscore methods. Now these are part of something called the data model, which is essentially an API that allows us to interact with Python's interpreter. Taking advantage of the data model makes our objects more intuitive and easier to work with. To demonstrate this, let's take a look at some code. So here I am in _04_03.py and right off the bat, I define a class called toppings. And in line two, I have the Dunder init method we discussed earlier. Now, if you think about it Dunder init is usually called by the interpreter when we instantiate an object. So it makes sense that it's part of that API that allows us to interact with the interpreter. On line five, we take advantage of the Dunder len method. Now, Dunder len is not a…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.