Today, a programmer is bound to come across object-oriented programming (OOP) during their career. As a modern programming language, Python provides all the means to implement the object-oriented philosophy. The Show
What Is Object-Oriented Programming?Object-oriented programming (OOP) is a programming pattern that consists in defining objects and interacting with them. An object is a collection of complex variables and functions and can be used to represent real entities like a button, an airplane, or a person. To declare, initialize, and manipulate objects in Python, we use classes. They serve as templates from which objects are created. The following diagram illustrates this idea: We can see in the above diagram that the dog class contains specific dog characteristics, such as breed and eye color, as well as the abilities to run and walk. Let’s start by defining a class in Python. What Is a Class?A class defines and structures all of the objects that are created from it. You can view the class as an object factory. Let’s take the example of a fox terrier—a breed
of dog. From an OOP standpoint, you can think of User-Defined And Special MethodsMethods are functions within a class that are designed to perform a specific task. Python differentiates between user-defined methods,
written by the programmer, and special methods that are built into the language. A user-defined method is a function created by the programmer to serve a specific purpose. For instance, a Special methods are identified by a double underscore at either side of their name, such as The ConstructorA constructor is a special method that the program calls
upon an object’s creation. The constructor is used in the class to initialize data members to the object. With our With an understanding of object oriented programming and classes, let’s now look at how the The Importance of Objects in PythonWe don’t always see it when we write a program, but objects are central to the way Python works. When we declare a simple variable in Python, an object is created in the background. If we execute the following bit of code:
How Does the __init__ Method Work?The Create a ClassLet’s begin by creating a class: class Dog: def __init__(self,dogBreed,dogEyeColor): self.breed = dogBreed self.eyeColor = dogEyeColor... First, we declare the class Dog using the keyword We’ll now see how to pass these parameters when declaring an object. This is where we need the keyword self to bind the object’s attributes to the arguments received. Create an ObjectNext we’ll create an object, or instance, of the class ...Tomita = Dog("Fox Terrier","brown")... When we create the object The
Access Object AttributesTo access an attribute of your brand new Fox Terrier object, you can use the dot (.) notation to get the value you need. A print statement helps us demonstrate how this works: ...print("This dog is a",tomita.breed,"and its eyes are",tomita.eyeColor) Executing the above code gives us the following result: This dog is a Fox Terrier and its eyes are brown The program accessed
The Default __init__ ConstructorIn Python, a constructor does not necessarily need parameters passed to it. There can be default parameters. A constructor with no mandatory parameters is called a default constructor. Let’s rewrite our class with a default constructor: class Dog: def __init__(self, dogBreed="German Shepherd",dogEyeColor="Brown"): self.breed = dogBreed self.eyeColor = dogEyeColor If a user does not enter any values, the constructor will assign “German Shepherd” and “Brown” as the attributes. We can now create an instance of Dog without specifying any parameter: Since there are no arguments to pass, we use empty parentheses after the class name. We can still display the object’s attributes: print("This dog is a",tomita.breed,"and its eyes are",tomita.eyeColor) This gives us the following output: This dog is a German Shepherd and its eyes are Brown This simple code works perfectly. Learn To Code With UdacityThe Want to really take your coding skills to the next level? Our Introduction to Programming Nanodegree program is your next step. We’ll teach you the foundations of coding and have you thinking and problem solving like a programmer! Complete Code ExampleExample 1:class Dog: def __init__(self, dogBreed,dogEyeColor): self.breed = dogBreed self.eyeColor = dogEyeColor tomita = Dog("Fox Terrier","brown") print("This dog is a",tomita.breed,"and his eyes are",tomita.eyeColor) Example 2:class Dog: def __init__(self): self.nbLegs = 4 tomita = Dog() print("This dog has",tomita.nbLegs,"legs")
|