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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:47:19+00:00 2026-06-12T18:47:19+00:00

I just make a foo class to explain what I mean: class Foo: def

  • 0

I just make a foo class to explain what I mean:

class Foo:
    def __init__(self, fakevalue):
        self.fv = fakevalue

    @staticmethod
    def make_a_foo():
        return Foo(2)

    def try_change_foo_value(self):
        self = Foo.make_a_foo()
        print "in change function, self.fv:", self.fv

if(__name__ =='__main__'):
    foo_instance = Foo(1)
    print "foo_instance.fv:", foo_instance.fv
    foo_instance.try_change_foo_value()
    print "foo_instance.fv:", foo_instance.fv

I expect:

foo_instance.fv: 1
in change function, self.fv: 2
foo_instance.fv: 2

But the result is:

foo_instance.fv: 1
in change function, self.fv: 2
foo_instance.fv: 1

We can see the self value has already changed, but the instance value does not.
Why? And how to solve this problem?

  • 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-12T18:47:20+00:00Added an answer on June 12, 2026 at 6:47 pm

    In this case, self is a pointer to the caller instance. Though you change the pointer in try_change_foo_value, it is just like changing a parameter in a function: it has no effect outside the method.

    To clarify this, you can consider

    a.try_change_foo_value() as shorthand for Foo.try_change_foo_value(a). This should make it obvious that when you change self, you are changing a local variable, while a, the caller, remains unchanged.

    To fix it, you should do

    def try_change_foo_value(self):
        self.fv = 2
    

    but as mgilson points out, it is more Pythonic to just say foo_instance.fv = 2.

    If you want to actually change the value of foo_instance (rather than just fv), you should do that elsewhere, not in a method (which makes little conceptual sense).

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

Sidebar

Related Questions

Is okay to break all dependencies using interfaces just to make a class testable?
In Scala, I can make a caseclass, case class Foo(x:Int) , and then put
Just assume I have some class Foo, that has two dependencies: an ISerializer<T> and
Consider such code (this is just example not real code): class Foo(url : String)
I know I could just make all the Mix_Musics public, and not worry about
I'm using the following code to just make a single UILabel on my cell.
Is the normal way to do this to just make 2 cols in my
Very new to javascript and html-type stuff. I wanted to just make a quick
I have to write my own hash function. If I wanted to just make
Just to make this clear, I have looked at the other questions on this

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.