class variables and instance variables in Python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 15

class variables and instance variables in Python

Hey, In challenges the answers concerning this topics are always surprising for me. The Python lessons are very short here. How are class variables declared and how are instance variables declared? Programming examples with traps&tricks are welcome.

13th Apr 2018, 7:33 AM
Oma Falk
Oma Falk - avatar
24 Answers
+ 24
Class variables are defined inside the class but, outside of a method's block. They are accessible to each instance of that class. For example: class Boy: gender = "male" new_boy = Boy() print(new_boy.gender) # prints "male" So, in the above example, the "gender" is our class variable. On the other hand, instance variable are defined inside a method of the class. They can vary with the instances. For example: class Boy: def __init__(self, name): self.name = name b1 = Boy("John") b2 = Boy("Rahul") b3 = Boy("Marsh") print(b3.name) # prints "Marsh" In the above example, "name" is our instance variable, which, as you can see, is varying with the instances!
13th Apr 2018, 5:27 PM
777
777 - avatar
+ 11
Rahul your examples are very helpful! Thanks a lot.
13th Apr 2018, 5:55 PM
Oma Falk
Oma Falk - avatar
+ 8
Tim aber sowas von! Vielen Dank!
13th Apr 2018, 5:52 PM
Oma Falk
Oma Falk - avatar
13th Apr 2018, 7:00 PM
Martin Möhle
Martin Möhle - avatar
+ 6
Martin very useful! danke!
13th Apr 2018, 7:36 PM
Oma Falk
Oma Falk - avatar
+ 6
Tim thanks for discussing your prog! "Der klügere gibt Nachhilfe"
13th Apr 2018, 8:07 PM
Oma Falk
Oma Falk - avatar
+ 6
ahhh I see! and in your program the Monsters are ronaldo, neymar and Kroos? (-:
5th May 2018, 10:48 AM
Oma Falk
Oma Falk - avatar
13th Apr 2018, 5:09 PM
Tim
Tim - avatar
+ 2
Hi guys, i m learning python. but ,i can't understand the 'res' operation ...pls explain me
14th Apr 2018, 12:35 AM
siva sankar
siva sankar - avatar
+ 2
class ClassName(object): self.instance_variable = value #value specific to instance class_variable = value #value shared across all class instances #accessing instance variable class_instance = ClassName() class_instance.instance_variable #accessing class variable ClassName.class_variable
14th Apr 2018, 6:44 AM
Oluwaseun Shaydus Oni
Oluwaseun Shaydus Oni - avatar
+ 2
siva sankar i dont even know about something that is called like this
14th Apr 2018, 7:50 AM
Tim
Tim - avatar
+ 2
With a class you can create many objects without writing much code. Like in a factory, objects can be mass-produced. Really useful in games, when you want to create 1000 monsters from scratch. https://code.sololearn.com/cjNkd8F0FSVB/?ref=app
5th May 2018, 9:39 AM
Johannes
Johannes - avatar
+ 1
What
22nd Sep 2019, 7:02 AM
Enes Karakaya
Enes Karakaya - avatar
+ 1
class Cat:#giving class a name Cat def __init__(self, color, legs):#def __init__ function, argument are self,color,legs. Self-it is mandatory to put self inside it for access data inside class.color&legs are of variables like name and age self.color = color#if any function in child class contains function like Cat(x,y) x will be taken as color and y will be as legs count. self.legs = legs felix = Cat("ginger", 4)#here color is Ginger and leg is 4 rover = Cat("dog-colored", 4)#color will be dog-colored legs will be 4. stumpy = Cat("brown", 3)#here color is brown and leg is 3 Very simple:p (stumpy has 3 legs lol 😂)
21st May 2020, 11:55 AM
NARESH
NARESH - avatar
+ 1
What is the use of document.write
16th Sep 2020, 9:32 AM
Harsh Kishore
Harsh Kishore - avatar
0
Hello
13th Sep 2019, 3:14 AM
TAUHEED AHMAD
0
Excuse me, I don't understand the =number they put in front of variables
28th Sep 2019, 11:41 PM
Aminu Jamiu Busayo
Aminu Jamiu Busayo - avatar
0
i dont know bro i am just a bigginer
4th Sep 2020, 2:09 PM
sailaja krovvidi
sailaja krovvidi - avatar
0
Hey guys i don't like it
18th Oct 2020, 7:56 PM
free fraa
free fraa - avatar
0
Глддщщ
5th Nov 2020, 10:23 AM
Kirill Suslov
Kirill Suslov - avatar