0

Why I get 7 as output whether I don't define self.x=x inside class?

class new: x=7 s=new() print(s.x)

16th Feb 2018, 9:08 AM
Azazul Islam
Azazul Islam - avatar
2 Réponses
+ 1
Please specify a certain language if it's python it is because you don't define the variable in a method. If you just declare it in the class outside of methods it is automatically a member of the class. If you would initialize x in the constructor you would have to use the self keyword... Also I think that the convention in python is to write class names with a capital letter at start.
16th Feb 2018, 9:20 AM
Chrizzhigh
Chrizzhigh - avatar
+ 1
Example: class MyClass: y = 2 def __init__(self) : self. x = 1 mclass = MyClass() # y is 2 and x is 1 (both are variables of MyClass)
16th Feb 2018, 9:28 AM
Chrizzhigh
Chrizzhigh - avatar