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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:31:50+00:00 2026-06-16T04:31:50+00:00

Very often, I find myself coding trivial datatypes like class Pruefer: def __init__(self, ident,

  • 0

Very often, I find myself coding trivial datatypes like

class Pruefer:
    def __init__(self, ident, maxNum=float('inf'), name=""):
        self.ident  = ident
        self.maxNum = maxNum
        self.name   = name

While this is very useful (Clearly I don’t want to replace the above with anonymous 3-tuples), it’s also very boilerplate.

Now for example, when I want to use the class in a dict, I have to add more boilerplate like

    def __hash__(self):
        return hash(self.ident, self.maxNum, self.name)

I admit that it might be difficult to recognize a general pattern amongst all my boilerplate classes, but nevertheless I’d like to as this question:

  • Are there any
    popular idioms in python to derive quick and dirty datatypes with named accessors?

  • Or maybe if there are not, maybe a Python guru might want to show off some metaclass hacking or class factory to make my life easier?

  • 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-16T04:31:52+00:00Added an answer on June 16, 2026 at 4:31 am
    >>> from collections import namedtuple
    >>> Pruefer = namedtuple("Pruefer", "ident maxNum name")
    >>> pr = Pruefer(1,2,3)
    >>> pr.ident
    1
    >>> pr.maxNum
    2
    >>> pr.name
    3
    >>> hash(pr)
    2528502973977326415
    

    To provide default values, you need to do little bit more… Simple solution is to write subclass with redefinition for __new__ method:

    >>> class Pruefer(namedtuple("Pruefer", "ident maxNum name")):
    ...     def __new__(cls, ident, maxNum=float('inf'), name=""):
    ...         return super(Pruefer, cls).__new__(cls, ident, maxNum, name)
    ... 
    >>> Pruefer(1)
    Pruefer(ident=1, maxNum=inf, name='')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Very often, I find myself using a callback function and I don't have its
I often find myself with code like private static final MyType sharedResource = MyType();
I often find myself in need of very complex SQL examples when testing abstraction
I find myself very often creating an object that has no public methods and
I find myself very often in the situation that I open an element in
I am not a very orderly person at times and I often find myself
I often find myself wanting to insert regular functions into a binded sequence. Like
I often find myself writing very similar code across projects. And more often than
In my day-to-day work, I often find myself writing classes like in this simplified
I often find myself writing very simple classes instead of C-style structs. They typically

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.