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

The Archive Base Latest Questions

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

Possible Duplicate: Python: How do I pass a variable by reference? I have the

  • 0

Possible Duplicate:
Python: How do I pass a variable by reference?

I have the following case:

class A:
    def __init__(self):
        self.test = ''
        self.func(self.test)
        print(self.test)
    def func(self,var):
        var = 'foo'

I want func to modify self.var and I’d like to be able to pass a self. to this method.

A bit like:

class A()
{
  public:
    char test[256];
    A() { func(test);}
  private:
    void func(char * var) { var = "foo"; }
};

I haven’t written C++ in a while but that’s sort of what I’m going for.

  • 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-14T09:01:48+00:00Added an answer on June 14, 2026 at 9:01 am

    Unfortunately, I don’t know C++ very well, but I’m guessing you want something like this:

    class A:
        def __init__(self):
            self.test = ''
            self.func("test")
            print(self.test)
        def func(self,var):
            setattr(self,var,'foo')
    

    We have to do it this way because we can change (mutate) self.test inside a function (if it’s mutable), but we can’t change which object it references (which is what you’re attempting to do with assignment). consider:

    def func1(x):
       x.append('foo')
    def func2(x):
       x = 'foo'
    
    a = []
    func1(a)
    print a #['foo']  #mutated a in func1
    func2(a)
    print a #['foo']  #didn't change a in func2, only created a new local variable named `x` and assigned it to the string 'foo'
    

    The only way around this is to pass some sort of proxy-like object which you can change. In this case, we pass the instance as the proxy object (self) and the attribute’s name (var) so we know what to change on self. Given those two pieces of information, we can make the desired changes — Of course, at this point, you’re probably best off getting rid of func all together and just using setattr directly.


    It’s probably also worth asking why you actually want to do this. In general, you can just do:

    self.test = "foo"
    

    why would you want to write:

    self.func(self.test)
    

    instead? I can understand if you’re trying to do that since you’re used to having private methods and attributes. But this is python. “We’re all consenting adults here.” Just rely on the conventions (prefix a variable with _ if you want to warn users against modifying it and prefix with __ to invoke name mangling to avoid collisions in superclasses).

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

Sidebar

Related Questions

Possible Duplicate: Python: How do I pass a variable by reference? I want to
Possible Duplicate: get python class parent(s) I have a: class Animal(models.Model): pass class Cat(Aminal):
Possible Duplicate: Why do you need explicitly have the self argument into a Python
Possible Duplicate: Python nested functions variable scoping After much trial and error I have
Possible Duplicate: Python reverse / inverse a mapping Say I have the following. D
Possible Duplicate: pass callback from python to c++ using boost::python I have to make
Possible Duplicate: Python, compute list difference I have two lists For example: A =
Possible Duplicate: Python: simple list merging based on intersections I have a multiple list:
Possible Duplicate: python random string generation with upper case letters and digits How do
Possible Duplicate: Python splitting strings and jumbling the middle I have a program in

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.