1

Below is the code

import os

class ABC:
    def test(self,name):
        var = 5
        var2 = 10
        dic = {"Ada": "adada"}
        print "asdfdsadv"
        var1 = "Ada"
        var2 = "asada"

obj = ABC()
print obj.test("Ada").var1

I am looking for something like this. Can I achieve this in python

I know this is var variable in local to class. Is there someway by using global or something else to acheive this

6
  • you need have a return function and call it; you can't call directly a variable Commented Sep 3, 2018 at 9:53
  • 1
    That would be a poor design. I have a feeling you have an XY problem Commented Sep 3, 2018 at 9:53
  • 1
    I suggest you to have a look on classes from this official python docs Commented Sep 3, 2018 at 9:53
  • 1
    No, that is a local variable. Commented Sep 3, 2018 at 9:53
  • Why negative marking for this question. I failed to understand sometimes. Negative marking should be done when the question is not clear. I have seen the pattern people giving negative rating just because they dont know the answer. Commented Sep 4, 2018 at 4:56

5 Answers 5

2

Accessing a variable from a class method is not possible, you have to set the variable at the class level like this:

import os

class ABC:
    def test(self,name):
        var = 5
        var2 = 10
        dic = {"Ada": "adada"}
        print "asdfdsadv"
        self.var1 = "Ada"
        var2 = "asada"

obj = ABC()
obj.test('Ada')
print obj.var1

You could chain obj.test('Ada').var1 in the same line by returning self into your test method.

Sign up to request clarification or add additional context in comments.

Comments

2

I think this would work. The init(self) behave like a constructor in other languages. So in effect I am constructing a class in a class, to make your last line work properly. But like other suggested that is not the way Python is supposed to be used.

import os

class ABC(object):
    def __init__(self):
        pass

    class test(object):
        def __init__(self,name):
            self.var = 5
            self.var2 = 10
            self.dic = {"Ada": "adada"}
            print ("asdfdsadv")
            self.var1 = "Ada"
            self.var2 = "asada"

if __name__ == "__main__":
    obj = ABC()
    print (obj.test("Ada").var1)

Comments

2

What you are looking for are the class variables, usually defined as self.variable. Here an example of your code:

import os

class ABC:
    def __init__(self):
        self.var = 5
        self.var2 = 10
        self.dic = {"Ada": "adada"}
        self.var1 = "Ada"
    def test(self,name):
        print self.var
        print self.var2
        print self.var1 + " " + name

obj = ABC()
print obj.dic # {'Ada': 'adada'}
print obj.dic["Ada"] # adada
obj.test("wow") # 5, 10, Ada wow
obj.var1 = "Ede"
obj.test("wow") # 5, 10, Ede wow

but as suggested in other answers, you may want to take a step back and check what is the scope of python variables

2 Comments

That wasn't what the OP asked. print does not return the attribute
I am not completely sure that the OP has the knowledge tu fully identify what he exactly need, so supposing an XY problem I gave the answer that I think will mostly help him. Plus, if you read the answer, you'll notice that the metod .test is here only as an example, meanwhile the part that returns the attribute is obj.dic["Ada"]; the rest is to provide a fast introduction to class variables
1

Forget about classes and consider functions in general.

When you define a function, any variables within its scope are local and only accessible from within the execution of that function. Once execution has finished, that's it, they are gone. This is fundamental; the only way of getting data from a function is by returning it.


Although it is a hack, you could return locals() (a dictionary of local variables), but this is terrible practice.

import os

class ABC:
    def test(self,name):
        var = 5
        var2 = 10
        dic = {"Ada": "adada"}
        print "asdfdsadv"
        var1 = "Ada"
        var2 = "asada"
        return locals()

obj = ABC()
print obj.test("Ada")["var1"]

Comments

1

If you return the object itself from the function and the variables are not local but instance variables it works.

class ABC:
    def test(self, name):
        self.var1 = "My name is {}".format(name)
        return self

obj = ABC()
print obj.test('Ada').var1

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.