Declaring new variables inside class methods
Asked Answered
G

5

14

I just read about class and method variables in Python, and I am wondering if there is a difference between these two examples:

class Example(object):
    def __init__(self, nr1, nr2):
        self.a = nr1
        self.b = nr2

    def Add(self):
        c = self.a + self.b
        return c

class Example2(object):
    def __init__(self, nr1, nr2):
        self.a = nr1
        self.b = nr2

    def Add(self):
        self.c = self.a + self.b
        return self.c

Basically if I do:

print Example(3,4).Add()
print Example2(3,4).Add()

I get the same result:

7
7

So my questions are:

  1. What is the difference between self.c = self.a + self.b and c = self.a + self.b?
  2. Should all new variables declared inside classes be declared with the self statement?

Thanks for any help!

Galvani answered 2/3, 2017 at 14:48 Comment(6)
self.c is a variable attached to the object whereas c is a local variable.Diacetylmorphine
@WillemVanOnsem: So I can declare new class instance variables inside methods?Galvani
Those are not class variables. These are object variables. Yes an object is basically a dictionary and you can add/modify/delete these items in the methods.Diacetylmorphine
I think the terminology is a bit mixed up in your question. If you want to add a class variables then use self.__class__.c = 10 (not recommended). self.c is an instance variable (except when self isn't an instance, self isn't a keyword in python!).Logbook
Also the question is a bit too broad (you already listed 4 questions, but each question should contain only 1 question) some of them are opinion-based (3. and 4.). You might consider narrowing the scope of the question(s) a bit.Logbook
@MSeifert: Thanks for the suggestions. I edited the postGalvani
H
3

The difference is that on the second example you store the result of the addition to an object variable c. In the first example, c is inside a method and thus can not be a class variable since it is instantiated when the Add method is called and not when the class is defined.

Variables inside a method should be written with self just if you want to store them. A local, unimportant variable can be written inside a method just like you did on example with c.

Histrionics answered 17/5, 2017 at 2:23 Comment(0)
D
5
class Example(object):
    def __init__(self, nr1, nr2):
        self.a = nr1
        self.b = nr2

    def Add(self):
        c = self.a + self.b
        return c

Suppose if we create a instance x=Example().

If we try to accces c using x.c . We would get following error

AttributeError: 'Example' object has no attribute 'c'.

Diverticulosis answered 6/12, 2017 at 7:25 Comment(1)
Although what you say is correct, You're not really explaining WHY it gives the errors, or WHY this answers his question. To improve this answer, please try to explain to the OP the reasons and logic behind your answer. You have to assume they don't know anything, so you're explaining it from a very simple level.Bursitis
H
3

The difference is that on the second example you store the result of the addition to an object variable c. In the first example, c is inside a method and thus can not be a class variable since it is instantiated when the Add method is called and not when the class is defined.

Variables inside a method should be written with self just if you want to store them. A local, unimportant variable can be written inside a method just like you did on example with c.

Histrionics answered 17/5, 2017 at 2:23 Comment(0)
P
2

Actually self.c is an instance variable (defined with in the method) while c is a local variable with a limited scope within the class (garbage collector). The difference is that self.c can be looked upon by other methods or class objects by calling the self.add() method first which can initialize the self.c instance variable, on the other hand c can never be accessed by any other method in anyway.

Patrizia answered 2/11, 2018 at 8:13 Comment(0)
C
0

1.- you can use self.c in other methods c no, is local variable.

2.- yes, with self.c.

3.- no, if you only need it in the current method you use local.

4.- inside __init__ or at the beginning of the class, it's ok.

Cupel answered 2/3, 2017 at 14:54 Comment(0)
S
0

Answer 1: The difference is that variable "c" is local to the method where it's defined and thus cannot be accessed or used outside the function.

Answer 2: It is not compulsory to define all instance variable in the __init__() method, Because objects are dicts you can add, delete and modify from methods.

Note: Instance variables are defined with self, which ties the variable to a specific object.

Smearcase answered 22/1, 2023 at 10:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.