0
Initialize variables of class
class Class: n=3 def __init__(self): n=5 C = Class() print (C.n) Its output 3 Object C of Class must call __init__ and n=5, but its output 3. Why?
10 Respuestas
+ 5
Use self.n = 5 instead of just n = 5. The reason is, when you do n = 5, n is treated as a local variable within __init__ method, to access the class variable n, you use self.n
Hth, cmiiw
(Edit)
self.<attribute> changes the value for a particular instance, to change the class' value we can use <Class>.<attribute>
+ 7
🍿nom, nom, nom, crunch, crunch. Don't mind me... I'm just eating popcorn waiting in suspense for Ipang to respond with a follow up. 😜
+ 6
Hey David Carroll where you've been, feels like yesterday was too long :D
Do me a favour to correct me answer here please, still struggling with that neck breaker Python XD
class P:
n = 3
def change_object_attr(self, val):
self.n = val
def change_class_attr(self, val):
P.n = val
a = P()
b = P()
print(P.n, a.n, b.n)
a.change_object_attr (10)
print(P.n, a.n, b.n)
a.change_class_attr (20)
print(P.n, a.n, b.n)
c = P()
print(P.n, a.n, b.n, c.n)
(Edit)
Popcorn looks yummy ...
+ 5
Александр Лебедев As much as I appreciate you marking my last response as the best answer, I would say that the original answer by Ipang is the best answer for your specific question.
My response provided more of a review which was inspired by Ipang and this topic in general.
+ 4
Hey Ipang ... For you... I've got plenty of popcorn to share. 🤪
Work and life have been keeping me busy. I've recently been making time for SoloLearn. We'll see how long my schedule let's me keep this up.
Regarding your answer, it looks like you've demonstrated the two types of variables in Python classes:
- Class Variables
- Instance Variables
Providing an explanation of the behavior differences in your code may be easier to understand with this code demo I've put together.
https://code.sololearn.com/cx8ReX9Dkhhl/?ref=app
DISCLAIMER: Since I don't actively work with Python, these are based on my observations and documentation I've reviewed. Hope this helps.
+ 4
Александр Лебедев Regarding your follow up question, the answer is: Yes.
You can see this examined further in the code I shared in my previous response.
+ 4
Big Thanks David Carroll for help with in-depth answer, and *ahem* ... popcorn XD
Good to see you around David, and to know that you've been busy outside, better be busy than idle I guess, still appreciate you being here, even for a short period as time permits : )
I actually could have said the same about the accepted answer mark, my proposed answer was a brief, an extended version actually deserved the appreciation for the fair effort given.
In any ways, I have always learned new things from the questions and the answers, and for that I Thank Александр Лебедев also : )
+ 2
So, can I set value for variable n of class Class (not object Class) inside body of the class? Just Class.n=5?
+ 2
To be honest, I haven't tried such ideas, and I'm not really sure whether Python has a way to differentiate class variables & methods with those of instance variables & methods.
I will search for that : )
+ 2
I thought, I can check as best answer more then one answer...., thanks