class variables and instance variables in Python | Sololearn: Learn to code for FREE!

+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.

4/13/2018 7:33:05 AM

Oma Falk

24 Answers

New Answer

+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!

+11

Rahul your examples are very helpful! Thanks a lot.

+8

Tim aber sowas von! Vielen Dank!

+8

Some more useful tips ;) https://code.sololearn.com/cHem7KP2mjU8/?ref=app

+6

Martin very useful! danke!

+6

Tim thanks for discussing your prog! "Der klügere gibt Nachhilfe"

+6

ahhh I see! and in your program the Monsters are ronaldo, neymar and Kroos? (-:

+2

https://code.sololearn.com/cyrTd1CrK27E/?ref=app ist das hilfreich?

+2

Hi guys, i m learning python. but ,i can't understand the 'res' operation ...pls explain me

+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

+2

siva sankar i dont even know about something that is called like this

+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

+1

+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 πŸ˜‚)

+1

What is the use of document.write

0

0

Excuse me, I don't understand the =number they put in front of variables

0

i dont know bro i am just a bigginer

0

Hey guys i don't like it

0

Π“Π»Π΄Π΄Ρ‰Ρ‰