I'm new to programming and trying to understand the term instance. I find that it's a strange word that I can't seem to grasp linguistically. ie. Even if you never heard the word Psychology before you can grasp it fairly quickly because you can understand that "psyco" refers to 'conditions of the mind', and "ology" refers to the 'study of something'.
But "Instance" in programming is so vague. It's a type of object. Ok great, but my understanding of object is still vague and inexperienced so I have nothing to relate it to conceptually.
It would be nice if someone with a non-technical background can explain "Instance" in English without using the word "Instance", or just saying that it's a type of object.
Perhaps someone can explain what they do. There are plenty of explanations of what they are (which I don't understand) and no answers from the point of view of what they actually do.
Thanks in advance to those who try to answer!
CodePudding user response:
I think of it as an "occurrence" of an abstract idea (object). As an example (bear with me) - the platonic Chair, an occurrence of Chair existing in nature (or instance of Chair) would be the dining room chair that I can just see past the entry way to my home office. In other words, the platonic Chair is the idea (or ideal) chair - the one that only exists in our minds, but my dining room chair that I can see (and touch) is the instance. So we can think of instance as a concrete example of an abstract idea.
CodePudding user response:
Think of a class as a blueprint from which objects are created. A blueprint is a detailed photographic print that shows how something will be built. Think of a blueprint of a house. This blueprint can be considered as a Class. With the same blueprint, we can create several houses, which can be considered as Objects. And each object, or house, built from this blueprint is an instance of that blueprint.
Let's take an 'Employee' class. You can consider this to be a template for the real-world entity employee. This class would have two things associated with it: Properties and Behaviors. The class ‘Employee’ would have certain properties associated with it such as First Name, Last Name, and Salary. Similarly, the class ‘Employee’ would have certain behaviors associated with it such as Create Full Name, Apply Raise, etc.
Objects are specific instances of a class. If ‘Employee’ is our class, then ‘Mario Rossi’, ‘Ashley Brown’ and ‘Gunther Schafer’ would be the specific instances of the class, or in other words, these would be the objects of the class ‘Employee’.
Creating a class and instances of a class
In the example below, we create a class named Employee. Then, we create two objects (emp1 and emp2), or instances, of this class.
class Employee:
pass
emp1 = Employee()
emp2 = Employee()
print(emp1)
print(emp2)
If you print these objects, you get this:
<__main__.Employee object at 0x000002297B231A00>
<__main__.Employee object at 0x000002297B1A8970>
This essentially shows the two objects have been created are unique instances, as they hold different places in memory.
With the class objects in place, we can now manually create instance variables for each employee. Say, for example, we want emp1 and emp2 to have a first name, a last name, an e-mail, and wage. All you need to do is to set the instance variables and pass their unique attributes. Now that each instance variable has its own unique attributes, you can print out the information you want.
class Employee:
pass
emp1 = Employee()
emp2 = Employee()
emp1.fname = 'Mario'
emp1.lname = 'Rossi'
emp1.email = '[email protected]'
emp1.wage = 100000
print(emp1.email)
print(emp1.wage)
Output:
[email protected]
100000
The code looks nice so far, but it's not practical. By having to set each variable manually, not only are we prone to make mistakes, but we are also not taking advantage of the power of classes. Instead, we are going to use a special function called init(). This function is a constructor from which we are going to assign values to object properties at the moment they're created.
class Employee:
def __init__(self,fname,lname,wage):
self.fname = fname
self.lname = lname
self.wage = wage
self.email = f"{fname}.{lname}@company.com"
emp1 = Employee('Mario','Rossi',100000)
emp2 = Employee('Jane','Smith',70000)
print(emp1.email)
print(emp1.wage)
Output:
[email protected]
100000
When we create our instances of our Employee class, we can pass in the values that we specified in our init() function. Now our init() function takes the instance which we call self. Self must always be the first parameter. Use the init() function to assign values to object properties