String Conversion in Python: When to Use __repr__ vs __str__ - - vimore.org

String Conversion in Python: When to Use __repr__ vs __str__

String Conversion in Python: When to Use __repr__ vs __str__

YouTube

https://dbader.org/python-tricks ► String conversion in Python classes and other advanced OOP techniques When you define a custom class in Python and then try to print one of its instances to the console (or inspect it in an interpreter session) you get a relatively unsatisfying result. The default "to string" conversion behavior is basic and lacking in detail. By default all you get is a string containing the class name and the `id` of the object instance (which is the object's memory address in CPython). That's better than *nothing*, but it's also not very useful. The solution here is adding the `__str__` and `__repr__` "dunder" methods (some call them "magic methods") to your class. They are the Pythonic way to control how objects are converted to strings in different situations. In this tutorial video I'll do a deep dive on how Python's to-string conversion works and how you can add it to your own custom classes. I'll walk you through the __str__ and __repr__ methods, when to use each, and some tips on how to use them in real world scenarios. Just remember: * The result of `__str__` should be readable. * The result of `__repr__` should be unambiguous. * Always add a `__repr__` to your classes. The default implementation for `__str__` just calls `__repr__`, so you get the best of both worlds. To learn how to use the full potential of Python check out "Python Tricks: The Book" at the link below: https://dbader.org/pytricks-book FREE COURSE – "5 Thoughts on Mastering Python" https://dbader.org/python-mastery PYTHON TRICKS: THE BOOK https://dbader.org/pytricks-book SUBSCRIBE TO THIS CHANNEL: https://dbader.org/youtube * * * ► Python Developer MUGS, T-SHIRTS & MORE: https://nerdlettering.com FREE Python Tutorials & News: » Python Tutorials: https://dbader.org » Python News on Twitter: https://twitter.com/@dbader_org » Weekly Tips for Pythonistas: https://dbader.org/newsletter » Subscribe to this channel: https://dbader.org/youtube



Method Types in Python OOP: @classmethod, @staticmethod, and Instance Methods

https://dbader.org/python-tricks ► Master OOP techniques in Python with bite-sized code examples What's the difference between @classmethod, @staticmethod, and

YouTube

7 Simple Tricks to Write Better Python Code

Visit http://python.cogsci.nl/ for more tutorials! Seven solutions to common small problems that Python programmers often solve in a suboptimal way. This video

YouTube

Python Context Managers and the "with" Statement (__enter__ & __exit__)

https://dbader.org/python-tricks ► Discover Python's advanced features and how to use them to your advantage The "with" statement in Python is regarded as an o

YouTube

Writing automated tests for Python command-line apps and scripts

https://dbader.org/python-tricks ► Write clean and testable Python command line apps and learn by example Two techniques for writing automated tests for your P

YouTube

💻 2017 Apple MacBook Pro 13" — Review for Software Developers

A review of Apple's 2017 MacBook Pro 13-inch (no Touch Bar) from a software developers perspective. ► Best-fitting Sleeve for the MacBook Pro 13in (2017): http

YouTube

Python Tutorial: List Comprehensions Step-By-Step

https://dbader.org/python-tricks ► See how to leverage Python's best features, like List Comprehensions, in your own programs This video tutorial breaks down P

YouTube

Python Tutorial: Iterators and Iterables - What Are They and How Do They Work?

In this Python Programming Tutorial, we will be learning about iterators and iterables. There is a lot of confusion around these terms and exactly what they mea

YouTube

A Crazy Python Dictionary Expression ?!

https://dbader.org/python-tricks ► Master advanced features in Python with free & easy to digest code examples We're going to pry apart this slightly unintuiti

YouTube

Python Tutorial: if __name__ == '__main__'

In this video, we will take a look at a common conditional statement in Python: if __name__ == '__main__': This conditional is used to check whether a python m

YouTube

Pylint Tutorial – How to Write Clean Python

https://dbader.org/python-tricks ► Get examples of clean and Pythonic code that passes any Pylint or PEP 8 run. In this Pylint tutorial video you'll see how to

YouTube

"is" vs "==" in Python – What's the Difference? (And When to Use Each)

https://dbader.org/python-tricks ► Write clean & Pythonic code and start using advanced features in your Python code It's easy to get tripped up by Python's "i

YouTube

Python: Lambda, Map, Filter, Reduce Functions

An intro to functional programming in Python 3 covering Lambda, Map, Filter and Reduce functions. ► Python Lambda Functions: https://youtu.be/Ob9rY6PQMfI ► Lam

YouTube

Python - 2019 Action plan to learn it - Step by step

Python is a great programming language and if you have decided that you will learn python in 2019, that's a great choice. Python is great for web scrapping, an

YouTube

Emulating switch/case Statements in Python with Dictionaries

https://dbader.org/python-tricks ► See how to emulate switch/case statements in Python and other "tricks" for advanced Pythonistas Python doesn't have switch/c

YouTube

Concept behind : if __name__ == "__main__"

This video demonstrates the concept behind the conditional statement: if __name__ == "__main__" Explore my tutorials: https://www.indianpythonista.tech/tutori

YouTube

Python Classes and Objects || Python Tutorial || Learn Python Programming

Classes are a fundamental part of modern programming languages. Python makes it easy to make a class and use it to create objects. Today you will learn the es

YouTube

Python OOP Tutorial 6: Property Decorators - Getters, Setters, and Deleters

In this Python Object-Oriented Tutorial, we will be learning about the property decorator. The property decorator allows us to define Class methods that we can

YouTube

Python Tutorial: File Objects - Reading and Writing to Files

In this Python Tutorial, we will be learning how to read and write to files. You will likely come into contact with file objects at some point while using Pytho

YouTube

Pythons __init__ method

This video tutorial looks at the use of the Python __init__ method used when writing classes from which objects are created and then initialized.

YouTube