Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8065905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:48:52+00:00 2026-06-05T11:48:52+00:00

I have a python class called Video, which represents YouTube videos. Given the ID

  • 0

I have a python class called Video, which represents YouTube videos. Given the ID of a YouTube video, Video returns an object representing that video. However, when a Video object is first created, YouTube is not queried. YouTube is only queried once an attribute is asked for that requires information from YouTube. Here’s how it works:

>>> from video import Video
>>> video = Video('B11msns6wPU') 
# 'B11msns6wPU' is the ID of a video
>>> video
Video(youtube_id="B11msns6wPU")
### As of now, no call to YouTube's API has been made
### Next, I ask for the title attribute. The object queries YouTube's API to get 
### this information. In doing so, the object is completely initialized
>>> video.title
u'Badly Drawn Boy - Disillusion (directed by Garth Jennings)'
>>> video.duration
u'275'
# no query was made to the API because the object has been already been initialized

I’m not sure if this is technically “lazy evaluation”, but it is similar in flavor. Video objects aren’t initialized until the first call for an attribute is made. I’m wondering if this technique was worth implementing. Obviously, it makes my code a bit more complicated. What are your thoughts?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T11:48:53+00:00Added an answer on June 5, 2026 at 11:48 am

    This is sort of a general judgement call, not a hard-and-fast decision.

    If the lazy object, the Video class instance, is central to your application then it might make sense to fully initialize it before doing anything else; if your app is useless without the data, get the data first.

    But if your app might have hundreds or thousands of these Video instances, and most of them will not be used, it doesn’t make sense to make the user wait while you init them all. In this case, it would make a lot of sense to defer the initialization until it is really needed. For example, if you will be showing a “wall of thumbnails” display, you might need the thumbnail for each video and the title but you might not need anything else unless the user clicks on a thumbnail.

    I do like the general approach, that the code using the object doesn’t need to know or care whether the object was pre-initialized or not.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Python class called ClassA and another Python class which is supposed
I have a model called Customer in google app engine with python: class Customer(db.Model):
I have a python class which reads a config file using ConfigParser: Config file:
You have a Python class which needs an equals test. Python should use duck-typing
Suppose I have a Python class that I want to add an extra property
I have a Python file with as content: import re import urllib class A(object):
Possible Duplicate: Circular (or cyclic) imports in Python I have class B that imports
I have written a class in python that implements __str__(self) but when I use
I have renamed a python class that is part of a library. I am
I have a Python project that has the following structure: package1 class.py class2.py ...

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.