I am a bit new to Oops and I am getting confused with Python classes.Though I studied C++ in high school, but I know what I studied back then wasn't C++ at all(It was kind of C with classes, no templates, STL, or namespace)
Correct me if I am wrong, but for me classes are just blueprints for objects, right?
Now if I wanted to create an instance of a class in C++(For eg, my class name is "animal")
I would do this:
animal lion;
or if I had a constructor(with arguments) then only I would do thisanimal tiger(4,5);
The problem I am facing is:
class animal:
val = 5
Its just a basic class, without any constuctor or methods:
Now with python I am able to reference animal as it is without creating any instances of it(I coudn't do that in C++, right?)
animal.val = 7
Another problem I am facing is,I need to use parenthesis always while creating instances, or it will refer to the class itself.
For eg;
lion = animal
lion.val = 6
tiger = animal
print tiger.val
and it prints 6,when I want to create an instance I've to use paranthesis, even though I haven't specified any constructor(Which wasn't necessary in C++), any special reason or just a matter of language syntax?
3 Answers 3
Everything in Python is an object. Classes are objects that create other objects.
Thus, when you reference animal.val, you're referencing the class property val on the class animal.
If you want to actually create an object of type animal, you'd call the class and get an instance back:
your_animal = animal()
Anything you set on that instance is then set for only that instance (whereas changes to the class are shared among all objects created via the class).
See here for more details about classes in Python.
6 Comments
__new__() method, and you almost never need to override it. You probably want to define __init__ to initialize instances in most cases. This is all covered by any python tutorial.Now with python I am able to reference animal as it is without creating any instances of it(I coudn't do that in C++, right?)
You have declared a static variable. As you can see it can get a little confusing without keywords. What you have done is the same as static in c++ classes
To create an instance variable it needs to be explicitly assigned to instance.
class animal:
def __init__(self):
self.val = 0
your_animal = animal()
your_animal.val = 3
animal.val is no longer globally available.
Comments
To show analogy to C++ (btw it's good practice to declare clases with Capital name, to distinguish class from member which is by convetion in lower case):
class Animal:
val = 4 # class variable
def __init__ (self, val = 0): # constructor (takes 0 or 1 argument)
self.member_val = val # instance variable
You call animal = Animal(5) to create new instance. Watch how Animal.val and animal.val differs:
animal = Animal(5)
print Animal.val, animal.member_val
>>> 4 5
Equivalent code in C++ would be:
class Animal {
public:
Animal() { // constructor with no arguments
this.member_val = 0;
}
Animal(int val) { // constructor with argument
this.member_val = val;
}
public:
int member_val; // member (instance) variable allocated for each instance
static int val = 4; // class level static variable
}
And class usage:
Animal animal = new Animal(5);
cout << Animal.val << " " << animal.member_val
>>> 5 4