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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:05:32+00:00 2026-06-09T03:05:32+00:00

I was working on a program in Python 2.7 and found myself attempting to

  • 0

I was working on a program in Python 2.7 and found myself attempting to pass a Python class field as a parameter within that same class. While I have modified my code to make it cleaner (and thereby eliminate the need for this construction), I am still curious.

For some examples (hugely simplified, but the concept is present):

[NB: For examples 1 and 2, suppose that I wanted to either take a number as input and increment it, or increment the current value.]

Example 1.

class Example:
    def __init__(self,x):
        self.value = x
    def incr(self,x=self.value):
        self.value = x + 1

Result:

"NameError: name 'self' is not defined"

Example 2.

class Example:
    def __init__(self,x):
        self.value = x
    def incr(self,x=value):
        self.value = x + 1

Result:

"NameError: name 'value' is not defined"

Example 3.

class Example:
    def __init__(self,x):
        ex2 = Example2()
        self.value = ex2.incr(x)
    def get_value(self):
        return self.value

class Example2:
    def __init__(self):
        self.value = 0
    def incr(self,x):
        return x + 1

ex = Example(3)
print ex.get_value()

Result:

4

To restate my question, why can’t I pass a Python class field as a parameter to its own method?

Let me know if you have any additional questions or would require more information. Thanks!

  • 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-09T03:05:33+00:00Added an answer on June 9, 2026 at 3:05 am

    Method default values are evaluated when the method is defined, not when the method is called. At that point the class is still being defined, and so no objects of that type exist. Therefore you cannot use self.

    You can use the workaround of having a default value of None and testing for this inside your method:

    def incr(self,x=None):
        if x is None:
            x = self.value
        self.value = x + 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a python program that I am currently working on which is working
I have a working Python based program that I want to run as a
I have a working python 2.7 program that calls a DLL. I am trying
Greetings, Forum. I'm working on a program in Python that uses Twisted to manage
I have a working program in C++ that generates data for a Mandelbrot Set.
I am working on a program using python, HTML and javascript. I have two
I'm working on a python program that will automatically combine sets of files based
I've written a working program in Python that basically parses a batch of binary
I'm working on a program in python on Windows 7 that matches features between
I am working on a python program that runs as an svn post-commit hook.

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.