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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T09:48:04+00:00 2026-05-21T09:48:04+00:00

Is there a cleaner way to get the defaults of a method than using

  • 0

Is there a cleaner way to get the defaults of a method than using the inspect module? I’d like to do this:

class C(object):
    def __init__(self,Type='generic',X=5,Y=500):
        defaults=getDefaults() #not sure about this line
        typeDefaults_X={'short':'1','long':'10'}
        typeDefaults_Y={'large':2000,'small':100}
        if X == defaults['X']: 
            X = typeDefaults_X.get(Type, defaults['X'])
        if Y == defaults['Y']: 
            Y = typeDefaults_Y.get(Type, defaults['Y'])

I know that I could do this by:

defaults=dict(zip(inspect.getargspec(C.__init__).args[1:],inspect.getargspec(C.__init__).defaults))

but it seems so terribly un-pythonic.

  • 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-21T09:48:05+00:00Added an answer on May 21, 2026 at 9:48 am

    It’s not really clear to me what you are trying to do. But in the method, variable already has the specified default value. But it seems to me that you would be better off not using formal default arguments in this case.

    class C(object):
    
        def __init__(self, Type='generic', X=None, Y=None):
            X = X or 5
            Y = Y or 500
            self.X, self.Y = {
                "generic": (X, Y),
                "short": (1, Y),
                "long": (10, Y),
                "large": (X, 2000),
                "small": (X, 100),
            }[Type]
    
        def __str__(self):
            return "X={0} Y={1}".format(self.X, self.Y)
    
    
    print(C())
    print(C('short'))
    print(C('long'))
    print(C('large'))
    print(C('small'))
    print(C('small', 10))
    print(C('small', 20, 20))
    

    Is clearer I think, but even this can be a little hard to follow. You might rethink your interface.

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

Sidebar

Related Questions

Is there a cleaner way to get the short version hash of HEAD from
I am wondering if there is a cleaner way to write the ( working
I'm writing a script using BaseHTTPRequestHandler class. And in do_GET(self) method I need to
When proxying an object and intercepting a method using castle dynamic proxy, is it
In Java it's written like this.. when I was porting this code... realizied there
I am trying to get SSE functionality in my vector class (I've rewritten it
I've seen on StackOverflow and googling around that the most used way to get
Hey there, Now im working this base on dmitko tutorial on extending django-registration post
Is there a way to test if a collection is already initialized? try-catch only?
I have been playing around with this and wanted to get a second opinion.

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.