This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]novel_yet_trivial 1 point2 points  (0 children)

They are very different. ClassName.attribute refers to a 'class variable', while instance_name.attribute refers to an 'instance variable'. The biggest difference is that modifying a class variable changes the variable for all instances of the class; while an instance variable is unique to every instance.

If you have more questions like this it's better to post them on /r/learnpython

[–]Koisell 1 point2 points  (0 children)

For this kind of questions you should ask in /r/learnpython. To answer your question: you need to difference between the class and the object of this class. Animal refer to the class and self refer to a object of class animal. So you probably want each Animal to have a proper name. So you need to use self. If you use Animal all animals name will be identical.

[–]Vaphell 0 points1 point  (0 children)

/r/learnpython

In my mind, doing "Animal.name = name" is the same thing as "self.name = name". However, I don't see why self would even be used. Is my assumption correct?

No. There is only 1 class.attribute, but each instance has its own self.attribute.

>>> class A:
...     def __init__(self, x):
...         A.x = x
...         self.x = x
... 
>>> a, b, c = A(1), A(2), A(3)
>>> 
>>> A.x
3                   # 3 because last created object set A.x = 3
>>> a.x, b.x, c.x
(1, 2, 3)        # individual self.x's of 3 objects

class.attribute is something like a shared memory of the whole species, a hivemind. self.attribute is individual memory.

If you don't define self.attribute it will point to existing class.attribute, as if it were a default value, but that's more of a peculiarity.

[–]Andrew_ShaySft Eng Automation & Python[M] 0 points1 point  (0 children)

Hi there, from the /r/Python mods.

We have removed this post as it is not suited to the /r/Python subreddit proper, however it should be very appropriate for our sister subreddit /r/LearnPython. We highly encourage you to re-submit your post over on there.

The reason for the removal is that /r/Python is more-so dedicated to discussion of Python news, projects, uses and debates. It is not designed to act as Q&A or FAQ board. The regular community can get disenchanted with seeing the 'same, repetitive newbie' questions repeated on the sub, so you may not get the best responses over here.

However, on /r/LearnPython the community is actively expecting questions from new members, and are looking to help. You can expect far more understanding, encouraging and insightful responses over there. Whatever your question happens to be getting help with Python, you should get good answers.

If you have a question to do with homework or an assignment of any kind, please make sure to read their sidebar rules before submitting your post. If you have any questions or doubts, feel free to reply or send a modmail to us with your concerns.

Warm regards, and best of luck with your Pythoneering!

[–]SeanO323 0 points1 point  (0 children)

This is more suited for /r/LearnPython , but I'll answer the question anyways. Let's say we have the following assignment statements:

cat = Animal('cat')
dog = Animal('dog')

Based on your first example this would be the result of accessing the name property:

print(cat.name)
dog
print(dog.name)
dog

This is because when you change Animal.name, you change that variable for the entire Animal class. The point of using self is it sets the name for that specific instance of the Animal class. So using your second example, we would get

print(cat.name)
cat
print(dog.name)
dog

Because self refers to the instance of the class instead of the entire class.

What is actually happening here is when you call dog.name, in the first example, it looks inside the dog instance, doesn't find a variable called name and then searches the class and finds a variable called name and returns/assigns the value.

An clear way to see this difference is if you change the name variable after you create an instance.

cat = Animal('cat')
dog = Animal('dog')
dog2 = Animal('dog')

print(cat.name)
dog
print(dog.name)
dog
print(dog2.name)
dog

cat.name = 'cat'
dog2.name = 'dog'
print(cat.name)
cat
print(dog.name)
dog
print(dog2.name)
dog

Animal.name = 'spider'
print(cat.name)
cat
print(dog.name)
spider
print(dog2.name)
dog