Python objects with special methods - Create better classes!
What you'll learn
- Explain the origin of the names Python datamodel, magic methods, dunder methods and special methods.
- Write an indexer to support subscripting with the __getitem__ method
- Make objects callable with __call__
- Define the object identity
- Apply __iter__, __next__ to make your objects iterable
- Specify object equality with __eq__, __ne__, __lt__, __gt__, __le__, __ge__
- Wrap existing code into context managers with __enter__ and __exit__
- Describe how objects are constructed by methods __new__ and __init__
Requirements
- Basic knowledge of classes and objects in Python
- Intermediate programming experience
Description
People say that python is an intuitive language to learn and use. Beginners write their first programs in no time and more advanced programmers make educated guesses how to use parts of Python they have never used before. Even third-party libraries seem to work exactly how you expected.
80% of our time we are using operators like +-/* to calculate, use for-loops to go through lists, use len() to count and indexers to access elements from lists. After a while you don't even have to think about these language features anymore. You just use them and expect things to work the same way everywhere.
But why and how does this work?
In this course, you learn how to design your classes and objects to support common language features so other programmers can make smart guesses about how to use them. You learn how to support iteration, subscripting (indexing), context managers, hashing, equality checking etc.
After this course, you are able to:
- Explain the origin of the names Python datamodel, magic methods, dunder methods and special methods.
- Write an indexer to support subscripting with the __getitem__ method
- Apply __iter__, __next__ to make your objects iterable
- Define the object identity
- Use __hash__ to make objects hashable
- Specify language featureswith special methods __eq__, __ne__, __lt__, __gt__, __le__, __ge__
- Make objects callable with __call__
- Describe how objects are constructed by methods __new__ and __init__
- Wrap existing code into context managers with __enter__ and __exit__
The length of this course is 2 hours. Each topic shows clear examples of how to make your classes more Pythonic by implementing special methods, the secret weapon of your objects. This course has no exercises.
This course is meant for developer with basic Python experience who want to improve their skills or developers who bring experience from another language like c#, java, javascript, c++ and want to learn about Pythons unique language features.
This course is taught by Loek van den Ouweland, a senior software engineer with 25 years of experience. Loek is the creator of Wunderlist for windows, Microsoft To-do and Mahjong for Windows and has 15 years of experience teaching software engineering to a wide variety of students.
Who this course is for:
- Python programmers who want understand Python better
- Python programmers who want to understand how language features like built-in functions and operators map to special methods in classes
Instructor
Loek van den Ouweland (Wunderlist, Microsoft Todo) is a born teacher. Right from the start of his career, he was told that a programmer helps his customers best when he shows what his products can do and how they are built.
He worked in many companies as programmer and trainer and enjoys to share the secrets of programming with others.
Loek has 25 years of experience training people with different backgrounds, all ages, working in branches ranging from medical systems to manufacturing and academics to aerospace.