|
| https://realpython.com/ |
| Start Here | https://realpython.com/start-here/ |
|
Learn Python
| https://realpython.com/lessons/checking-types-explicitly/ |
| Your Learning Plan →Your personalized Python learning plan | https://realpython.com/account/learning-plan/ |
| Python Tutorials →In-depth articles and video courses | https://realpython.com/search?kind=article&kind=course&order=newest&_from=nav_library |
| Learning Paths →Guided study plans for accelerated learning | https://realpython.com/learning-paths/ |
| Quizzes & Exercises →Check your learning progress | https://realpython.com/search?kind=exercise&kind=quiz&_from=nav_library |
| Browse Topics →Focus on a specific area or skill level | https://realpython.com/tutorials/all/ |
| Reference →Concise definitions for common Python terms | https://realpython.com/ref/ |
| Mentor AI →NewYour personal Python mentor | https://realpython.com/mentor/ |
| Office Hours →Live Q&A calls with Python experts | https://realpython.com/office-hours/ |
| Community Chat →Learn with other Pythonistas | https://realpython.com/community/ |
| Live Courses →Live, instructor-led Python courses | https://realpython.com/live/ |
| Books →Round out your knowledge and learn offline | https://realpython.com/products/books/ |
| Podcast →Hear what’s new in the world of Python | https://realpython.com/podcasts/rpp/ |
| Unlock All Content and Become a Python Expert →Get unlimited access to all Real Python has to offer | https://realpython.com/account/join/?from=navbar_learn |
| Unlock All Content →Become a Python Expert | https://realpython.com/account/join/?from=navbar_learn |
|
More
| https://realpython.com/lessons/checking-types-explicitly/ |
| Learner Stories | https://realpython.com/learner-stories/ |
| Python Newsletter | https://realpython.com/newsletter/ |
| Python Job Board | https://www.pythonjobshq.com |
| Meet the Team | https://realpython.com/team/ |
| Become a Contributor | https://realpython.com/jobs/ |
| Search | https://realpython.com/search |
| https://realpython.com/search |
| Join | https://realpython.com/account/join/ |
| Sign‑In | https://realpython.com/account/login/?intent=navbar&next=%2Flessons%2Fchecking-types-explicitly%2F |
| Unlock This Lesson | https://realpython.com/account/join/?utm_source=rp_lesson&utm_content=duck-typing-python |
| Unlock This Lesson | https://realpython.com/account/join/?utm_source=rp_lesson&utm_content=duck-typing-python |
|
| https://realpython.com/courses/duck-typing-python/#team |
| Getting to Know Duck Typing in Python | https://realpython.com/courses/duck-typing-python/ |
| Negar Vahid | https://realpython.com/courses/duck-typing-python/#team |
| Recommended Tutorial | https://realpython.com/duck-typing-python/ |
| Course Slides (.pdf) | https://realpython.com/courses/duck-typing-python/downloads/duck-typing-slides/ |
| Sample Code (.zip) | https://realpython.com/courses/duck-typing-python/downloads/duck-typing-code/ |
| Ask a Question | https://realpython.com/lessons/checking-types-explicitly/#discussion |
| https://realpython.com/feedback/survey/course/duck-typing-python/liked/?from=lesson-title |
| https://realpython.com/feedback/survey/course/duck-typing-python/disliked/?from=lesson-title |
| Contents | https://realpython.com/lessons/checking-types-explicitly/#description |
| Transcript | https://realpython.com/lessons/checking-types-explicitly/#transcript |
| Discussion | https://realpython.com/lessons/checking-types-explicitly/#discussion |
| 00:00 | https://realpython.com/lessons/checking-types-explicitly/#t=0.685 |
| Checking the types of an object explicitly is another | https://realpython.com/lessons/checking-types-explicitly/#t=0.685 |
| alternative to duck typing. | https://realpython.com/lessons/checking-types-explicitly/#t=3.165 |
| This approach is more restrictive | https://realpython.com/lessons/checking-types-explicitly/#t=5.275 |
| because it requires you | https://realpython.com/lessons/checking-types-explicitly/#t=7.085 |
| to make sure the object is a specific type | https://realpython.com/lessons/checking-types-explicitly/#t=8.365 |
| or has the necessary methods before using it in your code. | https://realpython.com/lessons/checking-types-explicitly/#t=11.265 |
| 00:15 | https://realpython.com/lessons/checking-types-explicitly/#t=15.745 |
| But what does it mean to check types explicitly? | https://realpython.com/lessons/checking-types-explicitly/#t=15.745 |
| When you’re checking types explicitly, you’re checking | https://realpython.com/lessons/checking-types-explicitly/#t=18.715 |
| an object’s type. | https://realpython.com/lessons/checking-types-explicitly/#t=22.125 |
| Instead of assuming it has the right methods, | https://realpython.com/lessons/checking-types-explicitly/#t=23.675 |
| you explicitly verify its type before using it. | https://realpython.com/lessons/checking-types-explicitly/#t=26.065 |
| 00:29 | https://realpython.com/lessons/checking-types-explicitly/#t=29.505 |
| You’re also making sure that it has the needed methods. | https://realpython.com/lessons/checking-types-explicitly/#t=29.505 |
| Before calling a method, | https://realpython.com/lessons/checking-types-explicitly/#t=32.945 |
| you check if the object actually supports it. | https://realpython.com/lessons/checking-types-explicitly/#t=34.345 |
| Okay, now how are you supposed to do this? | https://realpython.com/lessons/checking-types-explicitly/#t=37.795 |
| 00:41 | https://realpython.com/lessons/checking-types-explicitly/#t=41.015 |
| First, if you want to check whether an object comes from a | https://realpython.com/lessons/checking-types-explicitly/#t=41.015 |
| given class, you use the isinstance() function, | https://realpython.com/lessons/checking-types-explicitly/#t=44.245 |
| and when you want to check whether an object has a specific | https://realpython.com/lessons/checking-types-explicitly/#t=48.425 |
| method or attribute, you use the hasattr() function. | https://realpython.com/lessons/checking-types-explicitly/#t=51.345 |
| 00:58 | https://realpython.com/lessons/checking-types-explicitly/#t=58.085 |
| Let’s get practical. | https://realpython.com/lessons/checking-types-explicitly/#t=58.085 |
| This code defines two classes, Duck | https://realpython.com/lessons/checking-types-explicitly/#t=59.306 |
| and Pigeon, each with their own methods. | https://realpython.com/lessons/checking-types-explicitly/#t=62.175 |
| Both classes have a .fly() method, | https://realpython.com/lessons/checking-types-explicitly/#t=64.985 |
| meaning they can be used interchangeably when calling .fly() | https://realpython.com/lessons/checking-types-explicitly/#t=67.075 |
| or in other words, they partially support duck typing, | https://realpython.com/lessons/checking-types-explicitly/#t=70.695 |
| but the Pigeon class doesn’t have a .swim() method. | https://realpython.com/lessons/checking-types-explicitly/#t=74.215 |
| 01:18 | https://realpython.com/lessons/checking-types-explicitly/#t=78.525 |
| Let’s write some code to verify what happens. | https://realpython.com/lessons/checking-types-explicitly/#t=78.525 |
| After you save your code, you can head to the terminal. | https://realpython.com/lessons/checking-types-explicitly/#t=83.125 |
| 01:28 | https://realpython.com/lessons/checking-types-explicitly/#t=88.785 |
| Okay, let’s import Duck | https://realpython.com/lessons/checking-types-explicitly/#t=88.785 |
| and Pigeon first: from birds import Duck | https://realpython.com/lessons/checking-types-explicitly/#t=90.235 |
| and Pigeon. | https://realpython.com/lessons/checking-types-explicitly/#t=95.655 |
| Okay, let’s instantiate these classes | https://realpython.com/lessons/checking-types-explicitly/#t=98.065 |
| and store them in a list called birds. | https://realpython.com/lessons/checking-types-explicitly/#t=100.535 |
| 01:43 | https://realpython.com/lessons/checking-types-explicitly/#t=103.105 |
| birds = [Duck(), Pigeon()] | https://realpython.com/lessons/checking-types-explicitly/#t=103.105 |
| 01:50 | https://realpython.com/lessons/checking-types-explicitly/#t=110.195 |
| Now let’s loop through them and use the .fly() method on them. | https://realpython.com/lessons/checking-types-explicitly/#t=110.195 |
| for bird in birds: | https://realpython.com/lessons/checking-types-explicitly/#t=113.915 |
| bird.fly(). | https://realpython.com/lessons/checking-types-explicitly/#t=118.085 |
| It worked as expected. | https://realpython.com/lessons/checking-types-explicitly/#t=120.528 |
| 02:03 | https://realpython.com/lessons/checking-types-explicitly/#t=123.195 |
| The duck is flying, the pigeon is flying. | https://realpython.com/lessons/checking-types-explicitly/#t=123.195 |
| But what happens if you use the .swim() method | https://realpython.com/lessons/checking-types-explicitly/#t=125.915 |
| on the pigeon objects? | https://realpython.com/lessons/checking-types-explicitly/#t=129.595 |
| Let’s see what happens: for bird | https://realpython.com/lessons/checking-types-explicitly/#t=131.105 |
| in birds: bird.swim(). | https://realpython.com/lessons/checking-types-explicitly/#t=134.395 |
| 02:21 | https://realpython.com/lessons/checking-types-explicitly/#t=141.315 |
| You get the duck is swimming, | https://realpython.com/lessons/checking-types-explicitly/#t=141.315 |
| but you also get an AttributeError: | https://realpython.com/lessons/checking-types-explicitly/#t=143.355 |
| 'Pigeon' object has no attribute 'swim'. | https://realpython.com/lessons/checking-types-explicitly/#t=145.715 |
| 02:31 | https://realpython.com/lessons/checking-types-explicitly/#t=151.755 |
| Let’s use type checking | https://realpython.com/lessons/checking-types-explicitly/#t=151.755 |
| to avoid the AttributeError you just ran into | https://realpython.com/lessons/checking-types-explicitly/#t=152.885 |
| in the previous example. | https://realpython.com/lessons/checking-types-explicitly/#t=155.765 |
| Become a Member | https://realpython.com/account/join/ |
| https://realpython.com/lessons/using-abstract-base-classes-code/ |
| Overview | https://realpython.com/courses/duck-typing-python/ |
| https://realpython.com/lessons/checking-types-explicitly-code/ |
|
Getting Started With Duck Typing (Overview) 04:22
| https://realpython.com/videos/duck-typing-python-overview/ |
|
Exploring Duck Typing 03:11
| https://realpython.com/videos/exploring-duck-typing/ |
|
Examining Duck Typing and Polymorphism 03:48
| https://realpython.com/videos/examining-duck-typing-polymorphism/ |
|
Comparing Duck Typing in Python's Built-in Types 02:23
| https://realpython.com/videos/comparing-duck-typing-builtin-types/ |
|
Exercise: Toast Anything with Duck Typing 08:00
| https://realpython.com/lessons/exercise-toast-anything-duck-typing/ |
|
Exploring Alternatives to Duck Typing 01:27
| https://realpython.com/lessons/alternatives-duck-typing/ |
|
Using Abstract Base Classes 03:36
| https://realpython.com/lessons/using-abstract-base-classes/ |
|
Using Abstract Base Classes: Code 05:57
| https://realpython.com/lessons/using-abstract-base-classes-code/ |
|
Checking Types Explicitly 02:38
| https://realpython.com/lessons/checking-types-explicitly/ |
|
Checking Types Explicitly: Code 01:38
| https://realpython.com/lessons/checking-types-explicitly-code/ |
|
Tackling Type Hints 02:58
| https://realpython.com/lessons/tackling-type-hints/ |
|
Combining ABCs And Duck Typing 02:33
| https://realpython.com/lessons/abcs-duck-typing/ |
|
Combining ABCs And Duck Typing: Code 02:21
| https://realpython.com/lessons/abcs-duck-typing-code/ |
|
Exercise: Enforce an Appliance Interface with an ABC 09:00
| https://realpython.com/lessons/exercise-appliance-interface-abc/ |
|
Getting to Know Duck Typing in Python (Summary) 01:21
| https://realpython.com/lessons/duck-typing-python-summary/ |
| Privacy Policy | https://realpython.com/privacy-policy/ |
Viewport: width=device-width, initial-scale=1, shrink-to-fit=no, viewport-fit=cover