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

  • Home
  • SEARCH
  • 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 8519399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:14:16+00:00 2026-06-11T06:14:16+00:00

I see it’s not considered pythonic to use isinstance() , and people suggest e.g.

  • 0

I see it’s not considered pythonic to use isinstance(), and people suggest e.g. to use hasattr().

I wonder what the best way is to document the proper use of a function that uses hasattr().

Example:
I get stock data from different websites (e.g. Yahoo Finance, Google Finance), and there are classes GoogleFinanceData and YahooFinanceData which both have a method get_stock(date).
There is also a function which compares the value of two stocks:

def compare_stocks(stock1,stock2,date):
    if hasattr(stock1,'get_stock') and hasattr(stock2,'get_stock'):
        if stock1.get_stock(date) < stock2.get_stock(date):
            print "stock1 < stock2"
        else:
            print "stock1 > stock2"

The function is used like this:

compare_stocks(GoogleFinanceData('Microsoft'),YahooFinanceData('Apple'),'2012-03-14')

It is NOT used like this:

compare_stocks('Tree',123,'bla')

The question is: How do I let people know which classes they can use for stock1 and stock2? Am I supposed to write a docstring like "stock1 and stock2 ought to have a method get_stock" and people have to look through the source themselves? Or do I put all right classes into one module and reference that file in the docstring?

  • 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-11T06:14:18+00:00Added an answer on June 11, 2026 at 6:14 am

    I don’t see whats unpythonic about the use of isinstance(), I would create a base class and refer to the base class’ documentation.

    def compare_stocks(stock1, stock2, date):
        """ Compares stock data of two FinanceData objects at a certain time. """
        if isinstance(stock1, FinanceData) and isinstance(stock2, FinanceData):
            return 'comparison'
    
    class FinanceData(object):
        def get_stock(self, date):
            """ Returns stock data in format XX, expects parameter date in format YY """
            raise NotImplementedError
    
    class GoogleFinanceData(FinanceData):
        def get_stock(self, date):
            """ Implements FinanceData.get_stock() """
            return 'important data'
    

    As you see I don’t use duck-typing here, but since you’ve asked this question in regards to documentation, I think this is the cleaner approach for readability. Whenever another developer sees the compare_stocks function or a get_stock method he knows exactly where he has to look for further information regarding functionality, data structure or implementation details.

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

Sidebar

Related Questions

See my code <html> <body> <script type=text/javascript> var str=Visit Microsoft!; document.write( str = str.replace(,'ss'));
See Anonymous function below, I do not want go to next for loop iteration
See Deepa answer for the solution :) I read all of the solution given
See the picture below. It's a flash game from a well known website :)
See here: http://jsfiddle.net/zYcJm/ The jQuery should be validating each field to check if they
see fiddle Firstly i am new to jquery. i have to make one cell
see fiddle if patient has time slot alloted already then make color yellow using
see fiddle I want to drag the mouse on cell and whatever is under
See this question. How to show popup message like in stackoverflow I am trying
see fiddle i have html table and one textbox and one button.make cell selection

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.