3

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
2
  • 1
    A 'function' written in this context (inside a class definition) is known as a 'method'. In python, functions are 'instance' methods, by default. An 'instance method''s first parameter (in this case, self is always an 'instance' of the class in which it is defined (for example, self is a check1 object). By doing self.j = 5, you can make the value visible to anything that has a reference to this specific self. Jun 1, 2012 at 0:41
  • 1
    @Darthfett You should have written that as an answer.
    – Mattie
    Jun 1, 2012 at 1:20

3 Answers 3

7
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.
5
  • 2
    I wouldnt put much effort into answering this "question". The user asking has not once accepted an answer, left a single comment, or had any other activity after posting a question... Good answer though.
    – chown
    Jun 1, 2012 at 0:53
  • 1
    @chown : You're probably right, but even if the user doesn't have any activity, I can always hope that he/she will learn something from reading my post.
    – mgilson
    Jun 1, 2012 at 1:01
  • @chown the fact that I didn't comment on a question means that I accepted it. although I HAVE commented to a question or two before (when I still had confusions in it).
    – afroze
    Jun 1, 2012 at 7:21
  • @mgilson thanks for the solution! I can't use class variables for my particular solution. What I'm trying to achieve is have my google app engine keep working. There's i think a 30sec or so processing time out so i plan to redirect to another page in the middle of processing but i need to still access some variables from the previous page. I hope this solution works for it! (:
    – afroze
    Jun 1, 2012 at 7:21
  • But I have a feeling I might not be able to use this solution since apparently when i say something like "check_instance.helloworld()" it runs the entire function/method "helloworld" (correct me if I'm wrong) and I would be essentially back to square one? (Since I obviously don't want to process the entire function/method again in the redirected page.)
    – afroze
    Jun 1, 2012 at 7:30
0

j cannot be seen by another class; however, I think you meant self.j, which can.

class A(object):
    def __init__(self, x):
        self.x = x

class B(object):
    def __init__(self):
        self.sum = 0
    def addA(self, a):
        self.sum += a.x

a = A(4)
b = B()
b.addA(a)    # b.sum = 4
0
-2

Using class inheritane it is very easy to "share" instance variables

example:

class A:
    def __init__(self):
        self.a = 10

    def retb(self):
        return self.b

class B(A):
    def __init__(self):
        A.__init__(self)
        self.b = self.a

o = B()
print o.a
print o.b
print o.retb()
4
  • 2
    How does inheriting from a class allow you to share a variable defined on an instance?
    – mgilson
    Jun 1, 2012 at 0:48
  • I added an example of two classes sharing each others instance variables using inheritance
    – voscausa
    Jun 1, 2012 at 1:24
  • A.a is a class variable, not an instance variable.
    – mgilson
    Jun 1, 2012 at 1:36
  • It is an example. But I have changed the class variable in an instance in the example.
    – voscausa
    Jun 1, 2012 at 2:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.