0

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??

1
  • 2
    Can you explain why you are using @classmethod? (BTW, if you really do want class methods, the first argument should be called cls, not self, by convention and to make things clearer.) Commented Apr 4, 2014 at 17:50

3 Answers 3

1

nums is a class variable. Not an instance variable. If you want nums to be an object variable you have to declare into the __init__ method of the class(or some other method).

For instance:

class X:
    class_var = 3

    def __init__(self):
        X.class_var = X.class_var + 1
        self.instance_var =  3



>>> a = X()
>>> print(a.class_var)
>>> 4
>>> print(a.instance_var)
>>> 3

>>> b = X()
>>> print(b.class_var)
>>> 5
>>> print(a.instance_var)
>>> 3
Sign up to request clarification or add additional context in comments.

4 Comments

That's not a very helpful answer to someone who doesn't understand this situation.
self.nums is being assigned in OP's __init__.
@Wooble yes but assigned is not the same as declared that is what I said in my answer.
Considering Python doesn't have variable declaration, I don't see the distinction you're trying to make here.
0
class Sabirac(object):
    nums=[]
    def __init__(self):
            self.nums=[5,4]                
            for i in range (1,11):
                    self.add(randint(1, 100))

    def add(self,num):
            self.nums.append(num)

    def calc_sum(self):
            csum=0
            for num in self.nums:
                    csum=csum+num
            return csum

    def ispis(self):
            return self.nums

dont make your methods class methods ... it is clear you are lacking some of the knowledge about what these are

for example

class People:
     my_people = {}
     def __init__(self,name,age):
         self.name = name
         self.age = age
         People.my_people[name] = self    
     def birthday(self):
         #a birthday happens to an instance (or a person, not to people in general)
         self.age += 1
     @classmethod
     def get_person(cls,name):
         #this is asking the People class for an instance of a person
         return cls.my_people[name]

  billy = People("billy",8)
  billy.birthday()
  susan = People("susan",12)

  #somewhere else
  susan = People.get_person("susan") #call the classmethod
  susan.birthday()

Comments

0

On first glance, it seems irrelevant that we have a nums at class level:

it gets shadowed at class instantiation in __init__().

So each object has an instance variable with 5 and 4 in it, which you get with b.nums resp. c.nums.

But all your methods operate on the class, as you use @classmethod.

Thus, self should be replaced by cls, as it really represents the class.

And they modify the class attribute nums, which is shared by all instances.

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.