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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:47:27+00:00 2026-06-14T01:47:27+00:00

I would like to initialize a method’s parameter with some default value if an

  • 0

I would like to initialize a method’s parameter with some default value if an explicit value was not passed into the method – something like this:

class Example
   def __init__(self, data = self.default_data()):
      self.data = data

   def default_data():
      # ....
      return something

I got the error:

NameError: name 'self' is not defined

How do I fix this?

  • 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-14T01:47:28+00:00Added an answer on June 14, 2026 at 1:47 am

    The common idiom here is to set the default to some sentinel value (None is typical, although some have suggested Ellipsis for this purpose) which you can then check.

    class Example(object): #inherit from object.  It's just a good idea.
       def __init__(self, data = None):
          self.data = self.default_data() if data is None else data
    
       def default_data(self):  #probably need `self` here, unless this is a @staticmethod ...
          # ....
          return something
    

    You might also see an instance of object() used for the sentinel.

    SENTINEL = object()
    class Example(object):
       def __init__(self, data = SENTINEL):
          self.data = self.default_data() if data is SENTINEL else data
    

    This latter version has the benefit that you can pass None to your function but has a few downsides (see comments by @larsmans below). If you don’t forsee the need to pass None as a meaningful argument to your methods, I would advocate using that.

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

Sidebar

Related Questions

I would like to initialize boost::random::discrete_distribution with a double[] like this: boost::random::discrete_distribution<>* distribution(double* _distr)
I would like to define and initialize some variables in web.xml and the access
How I do Initialize this: public const int[][,] Map = ... I would like
I would like to unit test the Initialize method of a Controller object. The
I am a little confused here. I would like to do something like this:
I have a foreach method like this one: public void Initialize(ClassB fixed) { foreach
I would like to know how if it is possible to initialize some static
I would like to initialize all the components of the game engine with the
I would like to initialize an object in javascript.. My code is as follows:
I would like to know: how can i initialize class object with parameters? I've

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.