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 6332513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:16:41+00:00 2026-05-24T18:16:41+00:00

I have an interface that will be implemented by client code: class Runner: def

  • 0

I have an “interface” that will be implemented by client code:

class Runner:
    def run(self):
        pass

run should in general return a docutils node but because the far far most
common case is plain text, the caller allows run to return a string, which will be
checked using type() and turned into a node.

However, the way I understand “Pythonic”, this is not “Pythonic” because
checking the type() of something doesn’t let it “be” a type by “acting” like
one — ie “Pythonic” code should use duck typing.

I considered

def run_str(self):
    pass

def run_node(self):
    return make_node(self.run_str())

but I don’t care for this because it puts the not-so-interesting return type
right there in the name; it’s distracting.

Are there any ideas I’ve missed? Also, are there problems I might encounter
down the road with my “bad” system (it seems more or less safe to me)?

  • 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-05-24T18:16:41+00:00Added an answer on May 24, 2026 at 6:16 pm

    I think this is a slightly deceptive example; there’s something you haven’t stated. I’m guessing that when you say you “have an interface,” what you mean is that you have some code that accepts an object and calls its run method.

    If you aren’t testing for the type of that object before calling its run method, the you’re using duck typing, plain and simple! (In this case, if it has a run method, then it’s a Runner.) As long as you don’t use type or isinstance on the object with a run method, then you’re being Pythonic.

    The question of whether you should accept plain strings or only node objects is a subtly different question. Strings and node objects probably don’t implement the same interface at all! Strings fundamentally don’t quack like a node, so you don’t have to treat them like one. This is like an elephant that comes along, and if you want it to quack like a duck, you have to give the elephant a tape player and train the elephant to use it first.

    So this isn’t a matter of “duck typing” any more, but of interface design. You’re trying to decide how strict you want your interface to be.

    To give you an answer, then, at this level, I think it’s most Pythonic to assume that run returns a node object. There’s no need to use isinstance or type to test for that. Just pretend it’s a node object and if the programmer using your interface gets that wrong, and sees an exception, then they’ll have to read your docstring, which will tell them that run should pass a node object.

    Then, if you want to also accept strings, or things that quack like strings, you can do so. And since strings are rather primitive types, I would say it’s not inappropriate to use isinstance(obj, basestring) (but not type(obj) == str because that rejects unicode strings, etc.). Essentially, this is you being very liberal and kind to lazy users of your program; you’re already going above and beyond by accepting elephants as well as things that quack like ducks.

    (More concretely, I’d say this is a bit like calling iter on an argument at the beginning of a function that you want to accept both generators and sequences.)

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

Sidebar

Related Questions

We have an interface which will be implemented by classes that take care of
I have somewhat of a problem. We have a centralized interface engine that will
Let's say I have a class that implements the IDisposable interface. Something like this:
I have a class that implements specific interface ( IOrganicEnvironment<T, K> ) public class
I am writing client code in Scala that needs to interface with a framework
I have an interface that I have defined in C++ which now needs to
I have a interface that defines some methods with attributes. These attributes need to
I have an interface that defines some methods I would like certain classes to
I have an interface that needs to react to long key presses. That means
I have an interface that has large numbers of controls, see image below. Interface

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.