Let's consolidate the concepts of constructors and member methods Part 1

Let's consolidate the concepts of constructors and member methods Part 1

Lesson Details:
June 29, 2020


I: Introduction

The first question to ask yourself is "Why do I have to write an article about Programming Languages?”. The reason is that you are interested in programming, so you want to improve your knowledge. However, if you are not sure how to start it, you can read this article to find some recommendations.

A: Learn basic python programming for beginners

If you are a beginner in Python programming, the best way to learn something is doing exercises. You can complete some exercises on the Python official website. For example, you can try the following assignment:

https://www.python.org/doc/current/tut/node7.html

Review the language syntax and concepts of data types. Read Quiz 1 and solutions on the same website. To practice more, you can solve exercises on the same website. If you need help, you can use the following links:

http://www.learnpython.org/

http://www.codecademy.com/tracks/python

http://thinkpython.com/

https://docs.python.org/2/tutorial/index.html

II: Body

A: Let's consolidate the concepts of constructors and member methods part 1

Constructors are special methods that are called automatically when an object is created. They are used to initialize attributes of objects with initial values. Constructors are called with the constructor name new. So, they are often referred as "new-methods". When you create a class, you can define one or several constructors (you can also choose not to define any constructor). The constructors will be used to create new instances of the class; therefore, they must have the same name as the class (for example, class HelloWorld has only one constructor with the name HelloWorld). If you don't specify any constructor, Python will create a default constructor for you (it creates an empty method). A constructor will always return the instance of the class; therefore, it can't return another type of value (such as a string or an integer). If you create a function that returns an instance of a class, then this function will be considered as a constructor. You can see how this works in the following example:

class HelloWorld(): def __init__(self): print('Hello World') hello = HelloWorld() print(type(hello)) # hello2 = HelloWorld() # hello3 = HelloWorld() # hello4 = HelloWorld() # print(hello, hello2, hello3, hello4) # Hello World # # # # print(type(hello)) # print(type(hello2)) # print(type(hello3)) # print(type(hello4)) #

As seen in this example, there is a constructor with the name HelloWorld that returns an instance of this class and other functions that return an instance of this class too (they are all created by calling the constructor with the name HelloWorld). You can also see that all these instances have the same type but different addresses in memory (because each time a new object is created, a new address is assigned to it). In this case, we should consider that each object is different from every other object created from the same class (even if they have identical values in their attributes); therefore, what we call a copy of an object is just a new instance of a class with a new address in memory.

Another important thing to know about constructors is that if we have a function that returns an instance of a class and doesn't have the name of the class as its name, Python will create a default constructor for us. In other words, because we don't explicitly defined it, Python assumes that we want to create a new instance of the current class when we call this function without giving any argument or when we don't give any name after the parentheses () after calling this function (as seen in following examples):

class HelloWorld(): def __init__(self): print('Hello World') hello1 = HelloWorld() print(type(hello1)) # hello2 = HelloWorld() # hello3 = HelloWorld() # hello4 = HelloWorld() # print(hello1, hello2, hello3, hello4) # Hello World # # # # print(type(hello1)) # print(type(hello2)) # print(type(hello3)) # print(type(hello4)) #

You can also define different constructors for your classes with different names (for example, you can define one constructor named __init_(self) and one constructor named __init_(self, number)). However, if you don't specify any constructor, Python will create a default constructor for you (like in previous examples). You can see how this works in the following example:

class MultiNumber(): def __init_(self): self._sum = 0 def __init_(self, number): self._sum = number + self._sum def __init_(self): self._sum = 0 print('sum = {}'.format(self._sum)); print('instance of MultiNumber') mul1 = MultiNumber() print(type(mul1)) # mul2 = MultiNumber(5) print(type(mul2)) # mul3 = MultiNumber() print(type(mul3)) # print(mul1) # sum = 0 instance of MultiNumber print(mul2) # sum = 6 instance of MultiNumber print(mul3) # sum = 0 instance of MultiNumber

In this case, the constructor with the name __init_(self) is called when there is no argument or when there is no name after parentheses () after calling this function; otherwise it is called when there is an argument and when there is a name after parentheses () after calling this function as seen in this example:

print('sum = {}'.format(mul1._sum)) # sum = 0 instance of MultiNumber print('sum = {}'.format(mul2._sum)) # sum = 5 instance of MultiNumber print('sum = {}'.format(mul3._sum)) # sum = 0 instance of MultiNumber print('instance of MultiNumber') mul1 = MultiNumber() print('instance of MultiNumber') mul2 = MultiNumber() print('instance of MultiNumber') mul3 = MultiNumber() print('instance of MultiNumber') mul4 = MultiNumber() print('instance of MultiNumber') mul5 = MultiNumber() print('instance of MultiNumber') mul6 = MultiNumber() print('instance of MultiNumber') mul7 = MultiNumber() print('sum: {}'.format(mul1._sum)) # sum: 0 instance of MultiNumber print('sum: {}'.format(mul2._sum)) # sum: 0 instance of MultiNumber print('sum: {}'.format(mul3._sum)) # sum: 0 instance of MultiNumber print('sum: {}'.format(mul4._sum)) # sum: 0 instance of MultiNumber print('sum: {}'.format(mul5._sum)) # sum: 0 instance of MultiNumber print('sum: {}'.format(mul6._sum)) # sum: 0 instance of MultiNumber print('sum: {}'.format(mul7._sum)) # sum: 0 instance of MultiNumber

loader
Course content