Recent Posts

Class & Constructor





Python is an object-oriented programming language.
Almost everything in Python is an object, with its properties and methods.

A Class is like an object constructor or a "blueprint" for creating objects.
Classes are used to define a type like boolean, numbers or strings or complex types like list and dictionaries but sometimes there are complex concepts that are either one of those. Class is used to define new concepts types.

We define classes by using the class keyword, similar to how we define functions by using the def keyword.



These attributes have methods,and those methods belong to a particular object





Now look what happens if we create a customer2 without attributes


Each object is a different instance of the customer  class, as  you can see in this error

Now we will add an attribute


To recap:





 Constructors

The constructor method is used to initialize data. It is run as soon as an object of a class is instantiated.
In one of the previous examples, we show that we can not make a project without an X or Y coordinates. And we get the message:

"attribute error: customer object has no attribute x"



We will use a constructor to make or construct our object


Now we will apply the configuration to construct our project, and at the beginning, we will initialize the constructor

We will use the variable 50 and 70 to construct the object



Try to use Artist sing with constructor

First, we will define the body of our class



Now we will add a new method called the contructor


we will make it more interesting


Now we will change the Artist object

Each object is a different instance or person class







Class & Constructor Class & Constructor Reviewed by ohhhvictor on May 11, 2019 Rating: 5

No comments:

Facebook

 photo imagen120.jpg
Theme images by 5ugarless. Powered by Blogger.