René's URL Explorer Experiment


Title: Checking Types Explicitly (Video) – Real Python

Open Graph Title: Checking Types Explicitly – Real Python

Description: Checking the types of an object explicitly is another alternative to duck typing. This approach is more restrictive because it requires you to make sure the object is a specific type or has the necessary methods before using it in your code. But…

Open Graph Description: Checking the types of an object explicitly is another alternative to duck typing. This approach is more restrictive because it requires you to make sure the object is a specific type or has the necessary methods before using it in your code. But…

Opengraph URL: https://realpython.com/lessons/checking-types-explicitly/

X: @realpython

direct link

Domain: realpython.com


Hey, it has json ld scripts:
  {
    "@context": "https://schema.org",
    "@type": "VideoObject",
    "name": "Checking Types Explicitly",
    "description": "Checking the types of an object explicitly is another alternative to duck typing. This approach is more restrictive because it requires you to make sure the object is a specific type or has the necessary methods before using it in your code. But…",
    "thumbnailUrl": ["https://files.realpython.com/media/Duck-Typing-in-Python_Watermarked.b1b2472a1e39.jpg"],
    "uploadDate": "2025-03-11T14:00:00+00:00",
    "duration": "PT2M38S",
    
    "potentialAction": {
      "@type": "SeekToAction",
      "target": "https://realpython.com/lessons/checking-types-explicitly/#t={seek_to_second_number}",
      "startOffset-input": "required name=seek_to_second_number"
    }
  }
  

authorReal Python
theme-color#18354c
color-schemelight dark
twitter:cardsummary_large_image
twitter:imagehttps://files.realpython.com/media/Duck-Typing-in-Python_Watermarked.b1b2472a1e39.jpg
og:imagehttps://files.realpython.com/media/Duck-Typing-in-Python_Watermarked.b1b2472a1e39.jpg
twitter:creator@realpython
og:typevideo.episode
og:site_nameReal Python
apple-mobile-web-app-titleReal Python

Links:

https://realpython.com/
Start Herehttps://realpython.com/start-here/
Learn Python https://realpython.com/lessons/checking-types-explicitly/
Your Learning Plan →Your personalized Python learning planhttps://realpython.com/account/learning-plan/
Python Tutorials →In-depth articles and video courseshttps://realpython.com/search?kind=article&kind=course&order=newest&_from=nav_library
Learning Paths →Guided study plans for accelerated learninghttps://realpython.com/learning-paths/
Quizzes & Exercises →Check your learning progresshttps://realpython.com/search?kind=exercise&kind=quiz&_from=nav_library
Browse Topics →Focus on a specific area or skill levelhttps://realpython.com/tutorials/all/
Reference →Concise definitions for common Python termshttps://realpython.com/ref/
Mentor AI →NewYour personal Python mentorhttps://realpython.com/mentor/
Office Hours →Live Q&A calls with Python expertshttps://realpython.com/office-hours/
Community Chat →Learn with other Pythonistashttps://realpython.com/community/
Live Courses →Live, instructor-led Python courseshttps://realpython.com/live/
Books →Round out your knowledge and learn offlinehttps://realpython.com/products/books/
Podcast →Hear what’s new in the world of Pythonhttps://realpython.com/podcasts/rpp/
Unlock All Content and Become a Python Expert →Get unlimited access to all Real Python has to offerhttps://realpython.com/account/join/?from=navbar_learn
Unlock All Content →Become a Python Experthttps://realpython.com/account/join/?from=navbar_learn
More https://realpython.com/lessons/checking-types-explicitly/
Learner Storieshttps://realpython.com/learner-stories/
Python Newsletterhttps://realpython.com/newsletter/
Python Job Boardhttps://www.pythonjobshq.com
Meet the Teamhttps://realpython.com/team/
Become a Contributorhttps://realpython.com/jobs/
Searchhttps://realpython.com/search
https://realpython.com/search
Joinhttps://realpython.com/account/join/
Sign‑Inhttps://realpython.com/account/login/?intent=navbar&next=%2Flessons%2Fchecking-types-explicitly%2F
Unlock This Lessonhttps://realpython.com/account/join/?utm_source=rp_lesson&utm_content=duck-typing-python
Unlock This Lessonhttps://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 Pythonhttps://realpython.com/courses/duck-typing-python/
Negar Vahidhttps://realpython.com/courses/duck-typing-python/#team
Recommended Tutorialhttps://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 Questionhttps://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
Contentshttps://realpython.com/lessons/checking-types-explicitly/#description
Transcripthttps://realpython.com/lessons/checking-types-explicitly/#transcript
Discussionhttps://realpython.com/lessons/checking-types-explicitly/#discussion
00:00https://realpython.com/lessons/checking-types-explicitly/#t=0.685
Checking the types of an object explicitly is anotherhttps://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 restrictivehttps://realpython.com/lessons/checking-types-explicitly/#t=5.275
because it requires youhttps://realpython.com/lessons/checking-types-explicitly/#t=7.085
to make sure the object is a specific typehttps://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:15https://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 checkinghttps://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:29https://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:41https://realpython.com/lessons/checking-types-explicitly/#t=41.015
First, if you want to check whether an object comes from ahttps://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 specifichttps://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:58https://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, Duckhttps://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:18https://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:28https://realpython.com/lessons/checking-types-explicitly/#t=88.785
Okay, let’s import Duckhttps://realpython.com/lessons/checking-types-explicitly/#t=88.785
and Pigeon first: from birds import Duckhttps://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 classeshttps://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:43https://realpython.com/lessons/checking-types-explicitly/#t=103.105
birds = [Duck(), Pigeon()]https://realpython.com/lessons/checking-types-explicitly/#t=103.105
01:50https://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:03https://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() methodhttps://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 birdhttps://realpython.com/lessons/checking-types-explicitly/#t=131.105
in birds: bird.swim().https://realpython.com/lessons/checking-types-explicitly/#t=134.395
02:21https://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:31https://realpython.com/lessons/checking-types-explicitly/#t=151.755
Let’s use type checkinghttps://realpython.com/lessons/checking-types-explicitly/#t=151.755
to avoid the AttributeError you just ran intohttps://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 Memberhttps://realpython.com/account/join/
https://realpython.com/lessons/using-abstract-base-classes-code/
Overviewhttps://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 Policyhttps://realpython.com/privacy-policy/

Viewport: width=device-width, initial-scale=1, shrink-to-fit=no, viewport-fit=cover

Robots: max-image-preview:large


URLs of crawlers that visited me.