3

I am trying to call a variable defined in a function, inside another function of the same class, but with self doesn't work.

class Project():
    def function1(self):
        a='hello world,'

    def function2(self):
        b=self.a + ' I am alive'

Project1=Project()
print Project1.function1()
print Project1.function2()

python says: Project instance has no attribute 'a'.

I don't know very well how to use classes. I didn't use __init__ 'cause I do not have anything to put, is there maybe a way to add it even if I do not need it formally?

Thanks for your help.

4
  • 7
    in function1, a is local. You probably wanted self.a='hello world, ' Commented Dec 20, 2017 at 23:50
  • 1
    so, if I want to have the printing of function2, I should define a in function1 as self.a? Commented Dec 20, 2017 at 23:51
  • 1
    Yes. In your code as it is, self.a (attribute a of the object self) is never defined. a in function1 is local variable. See Classes - Python Scopes and Namespaces. Commented Dec 20, 2017 at 23:53
  • Thanks to everyone ;) now, it finally works Commented Dec 20, 2017 at 23:55

3 Answers 3

3

What you need is:

    def function1(self):
      self.a = 'hello world, '

Within function1, a is a local variable as someone stated, whereas self.a is an attribute attached to your current object.

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

2 Comments

and do I need to use init, or is not compulsory in a class?
It works the way it is. You only need __init__ if you want to set self.a right after the object is created (instantiated).
1

Your a and b variables are local. You can only use these variables in the method scope. If you want a class attribute (shared with all the class) you have to set the like self.a = ... and self.b = .... In python is not necessary create a constructor method __init__ neither initialize these attributes. But in your example if you call functions2 before function1 it will crash because you are using one attribute that doen't exist. Then is recommended initialize the attributes. You can initialize the attributes like this:

class Project:
    a = ''
    b = ''

    def function1(self):
        self.a='hello world,'

    def function2(self):
        self.b=self.a + ' I am alive'

More things to keep in mind:
1. The variables are in lower case and snake case: project1 = Project()
2. Your prints won't print anything because your functions don't return anything. You have to return something like:

def function1():
    return 'hello world,'

or if you need to set a and print:

def function1():
    self.a = 'hello world,'
    return self.a

Comments

0

In function1(self) you need to change a= to self.a=. In your original code you were creating a local variable a, you need to explicitly use self to attach the variable a to the instance of the object and stay persistent across function calls.

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.