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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:09:28+00:00 2026-05-26T20:09:28+00:00

I want to pass a default argument to an instance method using the value

  • 0

I want to pass a default argument to an instance method using the value of an attribute of the instance:

class C:
    def __init__(self, format):
        self.format = format

    def process(self, formatting=self.format):
        print(formatting)

When trying that, I get the following error message:

NameError: name 'self' is not defined

I want the method to behave like this:

C("abc").process()       # prints "abc"
C("abc").process("xyz")  # prints "xyz"

What is the problem here, why does this not work? And how could I make this work?

  • 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-26T20:09:29+00:00Added an answer on May 26, 2026 at 8:09 pm

    You can’t really define this as the default value, since the default value is evaluated when the method is defined which is before any instances exist. The usual pattern is to do something like this instead:

    class C:
        def __init__(self, format):
            self.format = format
    
        def process(self, formatting=None):
            if formatting is None:
                formatting = self.format
            print(formatting)
    

    self.format will only be used if formatting is None.


    To demonstrate the point of how default values work, see this example:

    def mk_default():
        print("mk_default has been called!")
    
    def myfun(foo=mk_default()):
        print("myfun has been called.")
    
    print("about to test functions")
    myfun("testing")
    myfun("testing again")
    

    And the output here:

    mk_default has been called!
    about to test functions
    myfun has been called.
    myfun has been called.
    

    Notice how mk_default was called only once, and that happened before the function was ever called!

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

Sidebar

Related Questions

I want to pass class method as and a default argument to another class
I want to pass any enum value to method in utility class and get
I want to pass an enum value as command parameter in WPF, using something
Here is my custom module using hook, Assume if I want to pass argument
I want to pass an integer value to a form in .Net so that
I want to pass in the tType of a class to a function, and
I want to pass a byte[] to a method takes a IntPtr Parameter in
I want to pass a few variables to another page. Currently I'm using response.redirect
I want to pass a variable to he buttonEvent method in the selector. [button
I've got a class function that needs to pass through a particular keyword argument:

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.