-4

Whats the difference between:

class Animal(object):
   pass

a = Animal;
a.asdas = 2;
print(vars(a))

output:

{'asdas' : 2}   

and

class Animal(object):
        pass

a = Animal();
a.asdas = 2;
print(vars(a))

output:

{'asdas': 2, '__doc__': None, '__dict__': <attribute '__dict__' of 'Animal' objects>, '__weakref__': <attribute '__weakref__' of 'Animal' objects>, '__module__': '__main__'}   

Why is the output so different, it seems that a=Animal() does not create the same object as a=Animal. Isnt this confuse?

From the answers the following got clear: It's very confuse when you come from the programming world like (C/C++). I was expecting a = Animal also calls the constructor with empty arguments...

Gabriel
  • 8,990
  • 6
  • 57
  • 101
  • did you try to look `type(a)`? – amyrit May 02 '15 at 21:08
  • [Class instantiation uses function notation in Python](https://docs.python.org/3.4/tutorial/classes.html#class-objects). Hence `A()` and `A` different things. – Ashwini Chaudhary May 02 '15 at 21:09
  • 2
    `a=Animal` doesn't create any object. It just says that the `a` variable reference points to the same memory place as the `Animal` class. – tomasyany May 02 '15 at 21:17
  • Why downvote, as a c++ programmer, this is just crude: since a = Animal would normally just call the default ctor, where in thepython world it is s absolutely not doing that... something learned (still confused) – Gabriel May 02 '15 at 21:43
  • I didn't downvote, but I would expect someone who is programming in other languages to read the documentation of a new programming language he's working with. If all languages were the same, why would we need them? – Matthias May 03 '15 at 08:41

6 Answers6

2

Compare the following:

a = Animal
b = Animal()

Which, if respectively and simply printing them out, gives:

<class '__main__.Animal'>
<__main__.Animal object at 0x108449940>

Thus: the first represents the class Animal, why the latter is an instance of the same.

gustafbstrom
  • 1,622
  • 4
  • 25
  • 44
1

a=Animal doesn't create any object. It simply assigns the class Animal to the name a.

Daniel
  • 42,087
  • 4
  • 55
  • 81
  • I think is more correct, given the confusion, to talk about _instance_ better than _ object_, eventhough when you understand it well you can use, by extension, _object_ for an instance. – tomasyany May 02 '15 at 21:14
1

When you do a = Animal you are actually saying the variable a is the same as the class (the object) Animal.

But when you do a = Animal(), Python is actually creating an instance of the class Animal in the memory. That means that a new instance of the Animal class is created and referenced at a special place in the memory. This instance will represent the class Animal behaviour, but will not be the class itself.

For further explanation about this fundamental computer science question, you can check this question: What is the difference between an Instance and an Object?.

Community
  • 1
  • 1
tomasyany
  • 1,132
  • 3
  • 15
  • 32
  • Why would you ever want to have a reference to the class itself. is that even in memory? in other languages only an instance is created on the heap some where with what we can work with, but why should you be able to work with the class itself??? That is where I did not understand python well I think. Python parses the file and creates definitions somewhere which can also be access, like `` a = Animal `` ? is that correct? – Gabriel May 02 '15 at 21:50
  • `a=Animal` doesn't create any new reference, it just make the `a` variable pointer to point to the same place as the `Animal` class. So, you think there is no winning, well, there can be. If your class is named `SuperLongClassNameBecauseIDontKnowHowToNameClasses` then the command `ShortName = SuperLongClassNameBecauseIDontKnowHowToNameClasses` would allow you to call the class with a shorter name. That is actually what happens when you do `import numpy as np`. You are just saying `import numpy` and then `np = numpy` in order to have a shorter name for it. – tomasyany May 03 '15 at 15:46
  • I can't imagine now a simple example where you would be interested in working with the class itself, but I guess sometimes you could need it. Maybe if you have a list of classes, where each one has a static method `same_method()` then you could do `for a in classes_list: a.same_method()`. There `a` would be taking the reference of every other class. That's a silly example, but something like that you could have, I guess. And about your second part of the question, I don't exactly know how does Python do with the memory, etc. But this is not a question about Python, is more general than that. – tomasyany May 03 '15 at 15:50
0

Why is it confusing? You are quite clearly doing different things here. When you do a = Animal(), you call the class which creates an instance of it. When you do a = Animal, you are simply making another reference to the class itself.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
0

In Python, functions (and classes) are objects, so you do anything to them you could do to an object, including assigning them to a variable name.

If you put () after the function name, you're calling it, and if it returns anything, that value will be assigned to the variable on the left side.

If you leave off the (), you're just assigning the function itself to a variable. You do this when you pass a function as a parameter, or if you want to create an 'alias' for a function.

If you do this a = A, then you can call the function with a() or A() with the same result.

1.618
  • 1,765
  • 16
  • 26
0

Using a = Animal is simply saying that a is Animal. For example:

>>> y = 1
>>> x = y
>>> print((x, y))
(1, 1)
>>> x == y
True
>>> x is y # the important part
True

And it works the same with classes:

>>> class Animal(object):
...     pass
...
>>> a = Animal
>>> print((a, Animal))
(<class '__main__.Animal'>, <class '__main__.Animal'>) # they are identical
>>> a == Animal
True
>>> a is Animal # again, the important part
True

When using a = Animal(), you are creating a class instance and assigning it to a. For example:

>>> class Animal(object):
...     pass
...
>>> a = Animal()
>>> print((a, Animal))
(<class '__main__.Animal'>, <__main__.Animal object at 0x022BF6F0>)
>>> a == Animal
False
>>> a is Animal
False

a is now an instance of Animal, not Animal itself.

>>> isinstance(a, Animal)
True
Zach Gates
  • 4,045
  • 1
  • 27
  • 51