0

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?

asked May 21, 2012 at 17:30

3 Answers 3

8

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.

answered May 21, 2012 at 17:35
Sign up to request clarification or add additional context in comments.

6 Comments

so using parenthesis is in fact just a language syntax, and doesn't refer to anything specific?
But then if I don't have a constructor, why do is still need the parenthesis?
@KartikAnand: You do have a constructor, you just didn't write it yourself.
@wooble so like in C++, if I specify my own constructor, will the default one get hidden as well?
The constructor is the __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.
|
2

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.

answered May 21, 2012 at 17:36

Comments

2

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
kindall
185k36 gold badges291 silver badges321 bronze badges
answered May 21, 2012 at 17:48

1 Comment

Thanks your analogy to C++ did help me with some of my queries

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.