4

I've been reading articles about OOP with python, specifically this one.

The autor of that article has a description and then a code example:

The Python syntax to instantiate a class is the same of a function call

>>> b = int()
>>> type(b)
<type 'int'>

By this I infer "instance" exist at the moment of the execution and not before. When you execute type(b) that's the instance of the class int().

But then I read this stack overflow answer:

Instance is a variable that holds the memory address of the Object.

Which makes me a little be confused about the term. So when I assign a variable at the moment of the execution the "instance" is created?

Finally this explanation in ComputerHope points to the fact that instances are the same as variable assigments:

function Animal(numlegs, mysound) {
this.legs = numlegs;
this.sound = mysound;
}
var lion = new Animal(4, "roar");
var cat = new Animal(4, "meow");
var dog = new Animal(4, "bark");

The Animal object allows for the number of legs and the sound the animal makes to be set by each instance of the object. In this case, all three instances (lion, cat, and dog) have the same number of legs, but make different sounds.

Could anyone actually provide a clear definition of when an instance exits?

das-g
  • 9,718
  • 4
  • 38
  • 80
Miguel Ortiz
  • 1,412
  • 9
  • 21
  • 2
    Possible duplicate of [what is meaning of instance in programming?](https://stackoverflow.com/questions/20461907/what-is-meaning-of-instance-in-programming) – Valentino Jan 18 '19 at 19:47
  • 5
    I don't think it's correct to make any definition of "instance" that uses the term "variable". You can instantiate objects without binding them to a name: `int()` by itself, with no assignment statement, is a perfectly valid Python program. An integer is instantiated, but there are no variables here. – Kevin Jan 18 '19 at 19:48
  • Every example you've provided is true! I would say that the term "instance" is an abstraction to begin with - there isn't much use in making a constrictive definition for it. You could very well define it by saying it has those three characteristics/behaviors. – KuboMD Jan 18 '19 at 19:50
  • [Python documentation](https://docs.python.org/3/tutorial/classes.html) says "Creating a new class creates a new type of object, allowing new instances of that type to be made." If that helps. Also, "Class instantiation uses function notation. Just pretend that the class object is a parameterless function that returns a new instance of the class." – Bill Jan 18 '19 at 19:51
  • The answer to your question "So when I assign a variable at the moment of the execution the "instance" is created?" is 'yes'. Both things happen at that instant. – Bill Jan 18 '19 at 19:53
  • This question is more oriented about "When" an instance is created more than "What" an instance is. Is a different question than the suggested as duplicate. – Miguel Ortiz Jan 21 '19 at 14:09

2 Answers2

1

Generally in OOP

Classes and objects are the two main aspects of object oriented programming. A class creates a new type where objects are instances of the class.

As explained here.

Thus everytime an object is created, it is called an instance.

Python makes no difference in this concept, however things are a little different from other languages like Java for instance.

In fact in Python everything is an object, even classes themselves.

Here is a brief explanation of how it works:

Considering this snippet:

>>> class Foo:
...     pass
... 
>>> type(Foo)
<type 'type'>
>>> 

Class Foo is type type which it is a metaclass for all classes in Python (There is however a distinction between 'old' and 'new' classes, more here, here and here).

Class type being a class, is an instance of itself:

>>> isinstance(type, type)
True

So Foo despite being a class definition, is treated like an object by the interpreter.

Objects as instances are created whit statements like foo = Foo(), foo being an object inherits from object class.

>>> isinstance(foo, object)
True

This class provides all the methods an object needs, such as __new__() and __int__() (new, init). In short the former is used to create a new instance of a class, the latter is called after the instance has been created and is used to initialize values like you did with Animal.

The fact that everything is an object also mean that we can do funny pieces of code like this one:

>>> class Foo:
...     var = 'hello'
... 
>>> foo = Foo()
>>> foo.var
'hello'
>>> foo.other_var = 'world'
>>> foo.other_var
'world'
>>> Foo.other_var
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: type object 'Foo' has no attribute 'other_var'
>>> Foo.var
'hello'
>>> 

Here I added an attribute on an object at runtime. That attribute will be unically in foo, the class itself or any other instances won't have it. This is called Instance variable and class variable.

Hope it all makes sense to you.

TL;DR

In Python everything (class definitions, functions, modules, etc..) are all treated like objects by the interpreter. Therefore 'everything' is an instance.

lch
  • 2,028
  • 2
  • 25
  • 46
  • 2
    I think that for the sake of simplicity, when replying to someone who doesn't know what an instance is, you can cut out a lot of the details of how in python classes are objects too and so on. – LtWorf Jan 18 '19 at 22:48
1

I've been reading articles about OOP with python, specifically this one.

The autor of that article has a description and then a code example:

The Python syntax to instantiate a class is the same of a function call

>>> b = int()
>>> type(b)
<type 'int'>

Also read the sentence before that:

Once you have a class you can instantiate it to get a concrete object (an instance) of that type, i.e. an object built according to the structure of that class.

So an instance of a class is an object that has that class as its type.

By this I infer "instance" exist at the moment of the execution and not before.

Yes, correct. "Instance" and "instance of" are runtime concepts in Python.

When you execute type(b) that's the instance of the class int().

Not quite.

The int instance here starts existing when int() is called.1 This process is what's called "instantiation" and the result (which is returned by this call, and in this example then assigned to b) is the "instance" of int.

But then I read this stack overflow answer:

Instance is a variable that holds the memory address of the Object.

Oh well, that's not quite correct. It's the object itself (the value at that memory address, if you will) that's the instance. Several variables may be bound to the same object (and thus the same instance). There's even an operator for testing that: is

>>> a = 5
>>> b = a
>>> a is b
True

Which makes me a little be confused about the term. So when I assign a variable at the moment of the execution the "instance" is created?

No, then the instance is bound to that variable. In Python, think of variables just as "names for values". So binding an object to a variable means giving that object that name. An object can have several names, as we saw above.

You can use an instance without assigning it to any variable, i.e., without naming it, e.g. by passing it to a function:

>>> print(int())
0

Finally this explanation in ComputerHope points to the fact that instances are the same as variable assigments:

function Animal(numlegs, mysound) {
this.legs = numlegs;
this.sound = mysound;
}
var lion = new Animal(4, "roar");
var cat = new Animal(4, "meow");
var dog = new Animal(4, "bark");

The Animal object allows for the number of legs and the sound the animal makes to be set by each instance of the object. In this case, all three instances (lion, cat, and dog) have the same number of legs, but make different sounds.

Unfortunately, that explanation on ComputerHope will probably confuse most readers more than it helps them. First, it conflates the terms "class" and "object". They don't mean the same. A class is a template for one type of objects. Objects and templates for a type of objects aren't the same concept, just as cookie cutters aren't the same things as cookies.

Of course, [for the understanding] it doesn't particularly help that in Python, classes are (special, but not too special) objects (of type type) and that in JavaScript until the class concept was introduced, it was customary to use plain objects as templates for other objects. (The latter approach is known as "prototype based object orientation" or "prototype based inheritance". In contrast, most other object oriented languages, including Python, use class-based object orientation / class-based inheritance. I'm not quite sure in what category modern ECMAScript with the class keyword falls.)

Could anyone actually provide a clear definition of instance?

Like I wrote further up: An instance of a class is an object that has that class as its type.

So an "instance" is always an "instance of" something. That also answers the linguistic take on the question in the title

When should I call it “instance”?

You should call it "instance" when you want to call it "instance of" something (usually of a class).


1 I haven't told the whole truth. Try this:

>>> a = int()
>>> b = int()
>>> a is b
True

Wait what? Shouldn't the two invocations of int have returned new instances each, and thus two distinct ones?

That what would have happened with most types, but some built-in types are different, int being one of them. The makers of the CPython implementation are aware that small integers are used a lot. Thus they let CPython create new ones all the time, they just have it re-use the same integer (the same object / instance) each time the same value is required. Because Python integers are immutable, that doesn't usually cause any problems, and saves a lot of memory and object-creation-time in computation-intensive programs.

The Python standard allows implementations to make this optimization, but AFAIK doesn't require them to. So this should be considered an implementation detail and your program logic should never rely on this. (Your performance optimizations may rely on it, though.)

das-g
  • 9,718
  • 4
  • 38
  • 80
  • Thanks for the detailed explanation, I actually was wondering about when the instance was "created", of course this is at the execution moment and not the variable assignment. So the author in the first link explained it correctly. It's clear that at least in python and this example the int() execution occurs first than the assignment, so is still a function execution. – Miguel Ortiz Jan 21 '19 at 14:04