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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:17:11+00:00 2026-06-17T10:17:11+00:00

How can I set a class variable from inside a function inside another function?

  • 0

How can I set a class variable from inside a function inside another function?

var.py

class A:
    def __init__(self):
        self.a = 1
        self.b = 2
        self.c = 3
    def seta(self):
        def afunction():
            self.a = 4
        afunction()
    def geta(self):
        return self.a

run.py

cA = A()
print cA.a
cA.seta()
print cA.a
print cA.geta()

python run.py

1
1
1

why does a not equal 4 and how can I make it equal 4?

Edit:

Thanks everyone – sorry, I just saw now. I accidentally was off by a _ in one of my names…. so my scope is actually all ok.

  • 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-17T10:17:13+00:00Added an answer on June 17, 2026 at 10:17 am

    The problem is that there are multiple self variables. The argument passed into your inner function overwrites the scope of the outer.

    You can overcome this by removing the self parameter from the inner function, and making sure you call that function in some way.

    class A:
        def __init__(self):
            self.a = 1
            self.b = 2
            self.c = 3
        def seta(self):
            def afunction():  # no self here
                self.a = 4
            afunction()       # have to call the function
        def geta(self):
            return self.a
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I call self.class.instance_variable_set(@var, ...) from inside a class method, where is that variable
In Ruby, can one object destroy another? For example: class Creature def initialize @energy
I am trying to set a variable value at an object (class) level from
I wan't to set a class variable of a class from the outside(via attr_accessor),
How can I set a select class and other attributes eg:events through the SelectList
I'm using Class based generic views, can anybody suggest me how can i set
Why can't I set $_SERVER['DOCUMENT_ROOT'] as attribute? see example code class foo { private
like in java I have: Class.getSuperClass().getDeclaredFields() how I can know and set private field
How can I can set a global variable for the username of the logged-in
The RFC for a Java class is set of all methods that can be

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.