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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:28:40+00:00 2026-05-16T22:28:40+00:00

I know that checking types in Python is bad and you should probably never

  • 0

I know that checking types in Python is bad and you should probably never do it. But I can’t seem to find the disadvantage to this.

class O(object):         
    def __init__(self, name):
        '''Can only be called in derived classes.'''
        if type(self) is O:
            message = "%(class)s cannot be instantiated, it must be derived."
            raise TypeError, message % { "class" : O }
        self.name = name

    def fn(self):
        '''Must be populated in derived classes.'''
        raise NotImplementedError

Now if someone tries to instantiate O, a class I never meant to be instantiated, they know immediately.

Is this still bad form?

  • 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-16T22:28:40+00:00Added an answer on May 16, 2026 at 10:28 pm

    Look at Abstract Base Classes as they will provide more fine grained control over how the subclasses are instantiated if this is something that you really want to do.

    All in all, this might be a valid use because you are not preventing me from passing whatever I want to your code but I still wouldn’t consider it pythonic. You are telling me that I can’t instantiate your class. What If I want to?

    Using ABC’s, it looks like:

    import abc
    
    class O(object):
        __metaclass__ = abc.ABCMeta     
        def __init__(self, name):
            self.name = name
    
        @abc.abstractmethod
        def fn(self):
            pass
    

    This has the advantage of not breaking super on the fn method. As you have it with the raise NotImplementedError, you are breaking super for multiple inheritance. If a class derives from two classes that subclass O and both call super (as they should to allow for multiple inheritance) then it will create the exception that you raise.

    So now, you are not just telling me that I can’t instantiate your class, you are telling me that I can’t use multiple inheritance when subclassing from your class.

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

Sidebar

Ask A Question

Stats

  • Questions 540k
  • Answers 540k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Why do you think this data is encrypted? This could… May 17, 2026 at 2:30 am
  • Editorial Team
    Editorial Team added an answer I think INSERT ON DUPLICATE KEY UPDATE is your best… May 17, 2026 at 2:30 am
  • Editorial Team
    Editorial Team added an answer Just had a closer look at your code and you're… May 17, 2026 at 2:30 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I know, type checking function arguments is generally frowned upon in Python, but I
I know this is a noob question, but I've worked with Python before and
I feel a little silly for asking this since I seem to be the
The situation I'm in is this - there's a process that's writing to a
So in PHP, I am checking a file extension against multiple types to get
I found a similar post about this but still not sure. As I am
I'm trying to use python ctypes to use these two C functions from a
The socket module in python wraps the _socket module which is the C implementation
I need to make a script that will take files from a user and
How to write a simple method, that checks whether a concrete type is a

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.