René's URL Explorer Experiment


Title: Checking Types Explicitly: Code (Video) – Real Python

Open Graph Title: Checking Types Explicitly: Code – Real Python

Description: Okay, so the first thing that you can try is the isinstance() function. This way, you’ll only be using the swim() method if the bird in question is a duck. So for bird in birds, if isinstance(bird,` Duck), meaning if the bird you’re looping through…

Open Graph Description: Okay, so the first thing that you can try is the isinstance() function. This way, you’ll only be using the swim() method if the bird in question is a duck. So for bird in birds, if isinstance(bird,` Duck), meaning if the bird you’re looping through…

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

X: @realpython

direct link

Domain: realpython.com


Hey, it has json ld scripts:
  {
    "@context": "https://schema.org",
    "@type": "VideoObject",
    "name": "Checking Types Explicitly: Code",
    "description": "Okay, so the first thing that you can try is the isinstance() function. This way, you’ll only be using the swim() method if the bird in question is a duck. So for bird in birds, if isinstance(bird,\u0060 Duck), meaning if the bird you’re looping through…",
    "thumbnailUrl": ["https://files.realpython.com/media/Duck-Typing-in-Python_Watermarked.b1b2472a1e39.jpg"],
    "uploadDate": "2025-03-11T14:00:00+00:00",
    "duration": "PT1M38S",
    
    "potentialAction": {
      "@type": "SeekToAction",
      "target": "https://realpython.com/lessons/checking-types-explicitly-code/#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-code/
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-code/
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-code%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-code/#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-code/#description
Transcripthttps://realpython.com/lessons/checking-types-explicitly-code/#transcript
Discussionhttps://realpython.com/lessons/checking-types-explicitly-code/#discussion
00:00https://realpython.com/lessons/checking-types-explicitly-code/#t=0.585
Okay, so the first thing that you can try is thehttps://realpython.com/lessons/checking-types-explicitly-code/#t=0.585
isinstance() function.https://realpython.com/lessons/checking-types-explicitly-code/#t=3.775
This way, you’ll only be using the swim() method if the birdhttps://realpython.com/lessons/checking-types-explicitly-code/#t=5.865
in question is a duck.https://realpython.com/lessons/checking-types-explicitly-code/#t=9.375
00:11https://realpython.com/lessons/checking-types-explicitly-code/#t=11.495
So for birdhttps://realpython.com/lessons/checking-types-explicitly-code/#t=11.495
in birds, if isinstance(bird,`https://realpython.com/lessons/checking-types-explicitly-code/#t=12.915
Duck), meaning if the bird you’re loopinghttps://realpython.com/lessons/checking-types-explicitly-code/#t=17.905
through is from the Duck class, then this bird can swim.https://realpython.com/lessons/checking-types-explicitly-code/#t=20.635
00:24https://realpython.com/lessons/checking-types-explicitly-code/#t=24.775
So bird.swim().https://realpython.com/lessons/checking-types-explicitly-code/#t=24.775
And there you go.https://realpython.com/lessons/checking-types-explicitly-code/#t=27.805
You get The duck is swimming without any errors.https://realpython.com/lessons/checking-types-explicitly-code/#t=29.745
You could also explicitly check if the current objecthttps://realpython.com/lessons/checking-types-explicitly-code/#t=32.945
or bird actually has the methodhttps://realpython.com/lessons/checking-types-explicitly-code/#t=36.265
that you’re trying to use on it.https://realpython.com/lessons/checking-types-explicitly-code/#t=38.685
00:40https://realpython.com/lessons/checking-types-explicitly-code/#t=40.785
You can use the hasattr() function for this.https://realpython.com/lessons/checking-types-explicitly-code/#t=40.785
So for birdhttps://realpython.com/lessons/checking-types-explicitly-code/#t=44.225
in birds, if hasattr(bird,https://realpython.com/lessons/checking-types-explicitly-code/#t=45.245
00:51https://realpython.com/lessons/checking-types-explicitly-code/#t=51.215
"swim"), meaning if the bird in question has a swim() method,https://realpython.com/lessons/checking-types-explicitly-code/#t=51.215
then bird.swim().https://realpython.com/lessons/checking-types-explicitly-code/#t=57.135
Let’s run this,https://realpython.com/lessons/checking-types-explicitly-code/#t=59.846
and you get the same thing,https://realpython.com/lessons/checking-types-explicitly-code/#t=61.825
The duck is swimming without any errors.https://realpython.com/lessons/checking-types-explicitly-code/#t=63.185
01:06https://realpython.com/lessons/checking-types-explicitly-code/#t=66.345
As you might have noticed, goinghttps://realpython.com/lessons/checking-types-explicitly-code/#t=66.345
with the hasattr() function gives you a more generic solutionhttps://realpython.com/lessons/checking-types-explicitly-code/#t=68.045
than isinstance().https://realpython.com/lessons/checking-types-explicitly-code/#t=72.075
And even though checking the type of an objecthttps://realpython.com/lessons/checking-types-explicitly-code/#t=74.105
before using it can be a good solutionhttps://realpython.com/lessons/checking-types-explicitly-code/#t=77.025
in some cases, it’s not always the best approach.https://realpython.com/lessons/checking-types-explicitly-code/#t=79.165
01:22https://realpython.com/lessons/checking-types-explicitly-code/#t=82.865
One of Python’s coolest characteristics is its flexibilityhttps://realpython.com/lessons/checking-types-explicitly-code/#t=82.865
with types, and checking types all the time isn’thttps://realpython.com/lessons/checking-types-explicitly-code/#t=86.915
exactly a flexible thing to do.https://realpython.com/lessons/checking-types-explicitly-code/#t=90.085
01:32https://realpython.com/lessons/checking-types-explicitly-code/#t=92.385
In the next few lessons, you’ll learn about more wayshttps://realpython.com/lessons/checking-types-explicitly-code/#t=92.385
to handle situations like this.https://realpython.com/lessons/checking-types-explicitly-code/#t=95.665
Become a Memberhttps://realpython.com/account/join/
https://realpython.com/lessons/checking-types-explicitly/
Overviewhttps://realpython.com/courses/duck-typing-python/
https://realpython.com/lessons/tackling-type-hints/
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.