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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:48:21+00:00 2026-06-10T13:48:21+00:00

I am writing a large amount of code and I haven’t got enough experience

  • 0

I am writing a large amount of code and I haven’t got enough experience on the best way of using variables in a python class and its functions so I thought I would ask for some opinions. Here is an example of how I could do it

class MyClass():
    vara = 'Class Variable A'

    def __init__(self):
        self.varb = 'Class instantiation Variable B'

    def a(self):
        varc = 2
        vard = 3

        print 'def a function variables unchanged', varc, vard

        MyClass.vara = 'Class Variable A now changed by def a'

    def b(self):
        print self.varb
        self.varb = 'Class instantiation Variable B now changed by def b'
        print self.varb

if __name__ == "__main__":
    print MyClass.vara

    a = MyClass()
    a.a()
    a.b()

    print MyClass.vara

    a.a()

This gives this result

Class Variable A
def a function variables unchanged 2 3
Class instantiation Variable B
Class instantiation Variable B now changed by def b
Class Variable A now changed by def a
def a function variables unchanged 2 3

My app in django is using a class with has around 25 functions. Its a test application and each function represents an API call. Each API call will give out a few variables based on the result and I need these variables set by the functions to be used by other functions – so in other words I need to call these local function variables by other functions. I want to know the best way to do this.

In the example above, I can set the class variable vara using a function within the class, by doing this MyClass.vara = 'something' or I can set the variable which is in the instantiation constructor, __init__. Then I can call these variables using any function.

What’s the better way, or is there an even better way to do this?

  • 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-10T13:48:22+00:00Added an answer on June 10, 2026 at 1:48 pm

    Not sure if I fully get what you’re asking, but if I understand you correctly, the answer is that it depends what you want to do. You should set the attribute on the class (as in your vara) if you want it to be stored once for the entire class. You should set it on the instance in __init__(as in your varb) if you want a separate attribute for each instance.

    The difference becomes clearer if you have a mutable object (like a list) as the value of the attribute. See below:

    >>> class Foo(object):
    ...     classVar = []
    ...     def __init__(self):
    ...         self.instVar = []
    >>> f = Foo()
    >>> g = Foo()
    >>> f.classVar
    []
    >>> f.instVar
    []
    >>> g.classVar
    []
    >>> g.instVar
    []
    >>> f.instVar.append(1)
    >>> f.instVar
    [1]
    >>> g.instVar
    []
    >>> f.classVar.append(1)
    >>> f.classVar
    [1]
    >>> g.classVar
    [1]
    

    Notice that in the last bit, mutating f.classVar also affected g.classVar. Because this is a class attribute, it is shared among all instances of the class. The instance attribute (instVar) is independent for each instance.

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

Sidebar

Related Questions

I am writing a server application which has a large amount of source code.
I spend a large amount of my time writing code dealing with images. I
I am writing an android application that requires a large amount of bandwith to
I am writing a test tool which places a large amount of load on
I'm writing a program using JOGL/openCL to utilize the GPU. I have code that
Background: I am writing a C++ program working with large amounts of geodata, and
We're writing a large production system in Java, and I'm considering whether or not
I am writing a fairly large C++ shared-object library, and have run into a
I am writing an application needs to use large audio multi-samples, usually around 50
I am writing an application needs to use large audio multi-samples, usually around 50

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.