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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:14:38+00:00 2026-06-01T06:14:38+00:00

Say I have this sample code I’m writing for a Python lesson I’m holding:

  • 0

Say I have this sample code I’m writing for a Python lesson I’m holding:

class Enum(dict):
  def __init__(self, *values):
    values = {mnemo: num
      for num, mnemo in enumerate(values)}
    dict.__init__(self, values) # ← this
  def __getattr__(self, name):
    return self[name]

This seems to work as expected, at least on a shallow method, but what I see most of the time on the literature on the Internet is:

  • Avoid inheriting from inbuilt types: it’s much better to type out proxy methods by hand!
  • Using super().method rather than explicitly naming superclass.method

I guess especially the latter is more DRY, but super() is going to be trickier to explain once I start throwing in multiple inheritance. I want to make sure, then, that there are no nasty gotchas in there that other people will then stumble upon.

  • 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-01T06:14:39+00:00Added an answer on June 1, 2026 at 6:14 am
    1. I don’t see why you should generally avoid to derive from built-in types. It was considered an improvement that the arrival of new-style classes enabled to do this, and even before this the standard library provided the UserDict and UserList classes to derive from. Of course you have to evaluate in every specific case whether inheritance or composition is what you need, but that’s how in always is in OOP — nothing special about built-in types here.

    2. I wouldn’t recommend super() in the example. It is only useful if all cooperating methods have the same signature, or at least cooperating signatures. For single inheritance and “diamond-free” multiple inheritance, explicitly calling base-class methods is fine in my opinion. If you want to get things right in multiple inheritance with diamonds, you are forced to use super(), but you have to design your methods carefully, see Raymond Hettinger’s “Python’s super() considered super!”

      It is generally safe to use super() in cases where all involved methods have exactly the same signature, including methods likes __getattr__() and __setattr__().

    3. Your example class is a rather questionable implementation of an Enum type, but not for any of the reasons you mentioned in your post. It mixes up two namespaces: The namespace of the attributes of dict and the enumeration entries. Try Enum("clear", "update") to see what I mean. In this particular case, the follwing implementation would be much better:

      class Enum(object):
          def __init__(self, *values):
              vars(self).update({mnemo: num for num, mnemo in enumerate(values)})
      

      This adds the enumeration entries as attributes to a standard instance without deriving from dict. Every custom class instance brings its own dictionary anyway, namely __dict__.

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

Sidebar

Related Questions

Let's say I have this code: $dom = new DOMDocument(); @$dom->loadHTMLFile('sample.html'); $xp = new
Lets say we have these 2 sample code : public synchronized void getSomething(){ this.hello
Say you have a webpage with following sample code: <tr class=even> <td>something1</td> <td colspan=1>somthing1.1</td>
Say I have this simple form: class ContactForm(forms.Form): first_name = forms.CharField(required=True) last_name = forms.CharField(required=True)
Let's say I have a very simple PrototypeJS class that looks like this: var
Lets say have this immutable record type: public class Record { public Record(int x,
Here's a sample of the scenario I'm facing. Say I have this column family:
This Twilio API Sample code isn't working in Rails 3: #voice_controller.rb def reminder @postto
Say I have the following code: import java.lang.InterruptedException; import javax.swing.SwingWorker; public class Test {
Say i have this sample XML. <result> <field k='field1'> <value h='1'><text>text_value1</text></value> </field> <field k='field2'>

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.