surendrapanday
surendrapanday

Reputation: 548

How do I reference a variable inside a function in a class in python from another external file in python?

I have a scenario where I have following definitions in class. Don't worry about indentation please:

class A(objectRef //imported from another file):
    def doSomething(self, object):
        self.var1 = object.get("something");

Then I have another file with functions, but my reference is outside function in fileB. FileB.py contains

1) ref = A().doSomething.var1
2) ref = A().doSomething(self.var1)
3) val = A().doSomething().val1
These did not work.

Unfortunately its not a function its variable reference which are used in for loops later. The variable is then Then, I need to extract var1 from another file where no function exist. Importing part is easy for class but then how do I reference to get var1 from another file.

doSomething() has hundreds of attributes referenced so unfortunately I won't be able to modify anything inside that. I would need to make things work from fileB.py as that is the one that needs to call class A()

Upvotes: 1

Views: 1667

Answers (2)

TheEagle
TheEagle

Reputation: 5992

Or if you want to do it in the form A().doSomething().var1, you can add

        return self

to your A.doSometing method.

Upvotes: 2

Anand Tripathi
Anand Tripathi

Reputation: 16126

If you want var1 then just do

obj = A()
obj.doSomething()
val = obj.var1

Upvotes: 4

Related Questions