-2

I google this question, but unfortunately I can't find answer of this question. If you have an article related to this question, pls share it.

class Dog:
    """A simple attempt to model a dog"""
    def __init__(self,name,age):
        self.name = name
        self.age = age

my_dog = Dog("Bob",8)

What is the instance in this question?

Jens
  • 8,423
  • 9
  • 58
  • 78
  • 3
    `my_dog` here is an instance of `Dog`. You can read more about classes [here](https://docs.python.org/3.8/tutorial/classes.html#class-objects). – Anton Kahwaji Dec 03 '19 at 10:49
  • The instance is `my_dog`. Creating an object from a class is called "instanciation", and the resulting object is an instance. It's a OOP-broad term, and googling "instance" is quite enough; have a read at that: https://en.wikipedia.org/wiki/Instance_(computer_science) – Right leg Dec 03 '19 at 10:49
  • Take a look at [this question](https://stackoverflow.com/questions/1215881/the-difference-between-classes-objects-and-instances) to understand the difference between class (type) and object/instance (of that type). Like `int` is a type and `1` is an instance of that type. – Jens Dec 03 '19 at 10:51
  • 1
    Does this answer your question? [What is the difference between an Instance and an Object?](https://stackoverflow.com/questions/2885385/what-is-the-difference-between-an-instance-and-an-object) – Sayse Dec 03 '19 at 10:51

4 Answers4

1

The Dog named Bob wich is 8 years old is the instance of the Class dog A Concreat dog wich was Created during the Code execution

my_dog is a Reference/ pointer to the Instance not the instance itself

you can have mutiple variables pointing at the same Instance example

my_dog = Dog("Bob", 8)
my_dog2 = my_dog

there is only one instance of the Dog named Bob but two pointers

Mutiple Variables pointing at the same Instance

Gorock
  • 391
  • 1
  • 5
0

my_dog is the instance here.

Think of class as a model/concept. Eg. A Car is a model which has 4 tyres , has more than 2 seats etc. Say when you actually see car or when an antual car is created , that car will be known as instance of the concept/model.

Faizan Naseer
  • 589
  • 3
  • 12
0

An individual object of a certain class is called Instance. In above example my_dog is an instance of class Dog. Hope this answers your question.

0

An instance is a concept in object oriented programming, it is not specific of python classes, I think it is quite well explained here: https://docs.oracle.com/javase/tutorial/java/javaOO/objectcreation.html

"When you create an object, you are creating an "instance" of a class, therefore "instantiating" a class."

In following line you are instantiating an object of class Dog and storing in my_dog variable.

my_dog = Dog("Bob",8)

In your example:

  • Dog is a class
  • my_dog is a variable where you store an instance (or object) of Dog class.
  • name and age are attributes of Dog class
carlosvin
  • 979
  • 9
  • 22