Hy people. I have one variable (at least one name) but when i try to access to in in different ways it is has different value. Here is code.
class Sabirac(object):
nums=[]
def __init__(self):
self.nums=[5,4]
for i in range (1,11):
self.add(randint(1, 100))
@classmethod
def add(self,num):
self.nums.append(num)
@classmethod
def calc_sum(self):
csum=0
for num in self.nums:
csum=csum+num
return csum
@classmethod
def ispis(self):
return self.nums
Here is output.
b=Sabirac()
b.nums
[5, 4]
b.ispis()
[85, 72, 6, 42, 34, 20, 77, 89, 91, 47]
c=Sabirac()
c.ispis()
[85, 72, 6, 42, 34, 20, 77, 89, 91, 47, 36, 61, 81, 41, 60, 42, 67, 56, 40, 99]
c.nums
[5, 4]
So I made class Sabirac and the acces directly to variable nums and have output
[5, 4]
After that I access same variable through method ispis(). However it give me completely different values
[85, 72, 6, 42, 34, 20, 77, 89, 91, 47]
Then if I create new instance of class c=Sabirac() and call c.ispis() my output will be:
[85, 72, 6, 42, 34, 20, 77, 89, 91, 47, 36, 61, 81, 41, 60, 42, 67, 56, 40, 99]
note that is just append 10 values to b.ispis().
Could someone please tell me what is going on here??
@classmethod? (BTW, if you really do want class methods, the first argument should be calledcls, notself, by convention and to make things clearer.)