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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:37:01+00:00 2026-06-14T08:37:01+00:00

I know the question header sounds weird, but since English is not my first

  • 0

I know the question header sounds weird, but since English is not my first language, I find it very hard to formalize. However, I might be able to explain it with bit more text.

The problem is, that I’m trying to create a class called “Foo” for example.

# ../myProject/Foo.py

class Foo:
    '''Represents an example class for stackoverflow'''

Now all of Foo class’ instances have function attribute, which simply holds a function which can be executed via the instance. Also, there’s a parameters attribute, a tuple or a list, which holds parameters which should be used when the function gets called.

    def __init__(self, function, parameters):
        self.function = function
        self.parameters = parameters

    def callFunction(self):
        if self.function:
            self.function(*self.parameters)

This seems to be working fine, however, the problem is, that I want to give it a default value, to change an attribute of the instance. I basically wanna do the following:

def __init__(self, function=setattr, \
        parameters=(self, "togglableAttribute", not self.togglableAttribute)):

And doing this will raise NameError: name 'self' is not defined. How should I implement this in order for it to work, or should I come up with a workaround?

  • 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-14T08:37:02+00:00Added an answer on June 14, 2026 at 8:37 am

    self is the typical variable name used to describe the instance. However, default arguments are evaluated when the function is created (at class creation time). Of course self doesn’t exist yet because the class doesn’t even exist yet — The class is still in the process of being built.

    The typical way to do this is to check for a sentinel;

    def __init__(self, function=setattr, parameters=None):
        if parameters is None:
           parameters = (self, "togglableAttribute", not self.togglableAttribute)
    

    See my answer here (and the comments below it) for a discussion of various objects that you can use as your sentinel and the various pros and cons of each.

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

Sidebar

Related Questions

I know this is a very common question, but I could not find the
I know the question is not very descriptive but I couldn't phrase it better.
I know this question might be little silly but I can't seem to find
Such a newbie question I know but I can't seem to find any answers
I know this question has been asked many times before but I can't find
I know this is a very simple question but I have been struggling with
I just wanna ask this very trivial question, I do not know if this
This sounds like a trivia question but I really need to know. If you
I know this question has been asked 100x but I can't seem to find
I know this question is very similar to this one, but I feel its

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.