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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:36:01+00:00 2026-06-05T01:36:01+00:00

I want to use the variables i have declared inside a function in one

  • 0

I want to use the variables i have declared inside a function in one class, in another class.

For example i want to use the variable “j” in another class. Is it possible? (I read somewhere that it might have something to do with instance variables but fully couldn’t understand the concept).

class check1:
    def helloworld(self):
        j = 5
  • 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-05T01:36:02+00:00Added an answer on June 5, 2026 at 1:36 am
    class check1:
        def helloworld(self):
            self.j = 5
    
    check_instance=check1()
    print (hasattr(check_instance,'j'))  #False -- j hasn't been set on check_instance yet
    check_instance.helloworld()          #add j attribute to check_instance
    print(check_instance.j)  #prints 5
    

    but you don’t need a method to assign a new attribute to a class instance…

    check_instance.k=6  #this works just fine.
    

    Now you can use check_instance.j (or check_instance.k) just like you would use any other variable.

    This may seems a little bit like magic until you learn that:

    check_instance.helloworld()
    

    is completely equivalent to:

    check1.helloworld(check_instance)
    

    (If you think about it a little bit, that explains what the self parameter is).

    I’m not completely sure what you’re trying to achieve here — There are also class variables which are shared by all instances of the class…

    class Foo(object):
        #define foolist at the class level 
        #(not at the instance level as self.foolist would be defined in a method)
        foolist=[]  
    
    A=Foo()
    B=Foo()
    
    A.foolist.append("bar")
    print (B.foolist)  # ["bar"]
    print (A.foolist is B.foolist) #True -- A and B are sharing the same foolist variable.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a variable $cartTotal in cart.php page i want to use that same
I want to use a bunch of local variables defined in a function, outside
I want to use a variable in a regex, like this: variables = ['variableA','variableB']
I am developing one web application. In that i want to use variable's value
I have an array of type string which I want to re-use inside a
I want to know all the variables that have been declared within an include
I have declared a class variable in here void downloader_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e) {
Lets say that i have a class with some instance variables and i want
I have declared a local variable named cont in a function named validate. I
Is it possible to use Mathematica's manipulate to change variables that have already been

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.