Skip to main content

More about object-oriented programming


 

Note: This reading contains only a brief introduction to object-oriented programming. A more detailed discussion about the nuances of object oriented programming is beyond the scope of this course.

Previously, we identified object-oriented programming as a programming schema that is based around objects, which can contain both data and code that manipulates that data. You may recall that a class is an object’s data type that bundles data and functionality together, and you’ve encountered some examples of this class-specific functionality in the form of methods and attributes. In this reading, you’re going to learn more about object-oriented programming and how it works. Although this certificate program will not require you to define your own classes, having a basic understanding of how this process works will be very helpful when you encounter these concepts along your learning journey.

Review: Attributes and methods

Python classes are powerful and convenient because they come with built-in features that simplify common data analysis tasks. These features are known as attributes and methods.

  • Attribute: A value associated with an object or class which is referenced by name using dot notation.

  • Method: A function that belongs to a class and typically performs an action or operation. 

A simpler way of thinking about the distinction between attributes and methods is to remember that attributes are characteristics of the object, while methods are actions or operations.

For example, if the class were Spaceship, then attributes might be:

name

kind

speed

tractor_beam

These attributes could be accessed by typing:

Spaceship.name

Spaceship.kind

Spaceship.speed

Spaceship.tractor_beam

Notice that these characteristics are accessed using only a dot. 

On the other hand, methods of the Spaceship class might be:

warp()

tractor()

These methods could be used by typing:

Spaceship.warp() 

Spaceship.tractor()

Notice that methods are followed by parentheses, and it’s possible for them to take arguments. For example, Spaceship.warp(7) could change the speed of the ship to warp seven.

Defining classes with unique attributes and methods

Python lets you define your own classes, each with their own special attributes and methods. This helps all different kinds of programmers to build reusable code that makes their work more efficient. You can even build the Spaceship class mentioned previously. The example, here, demonstrates how to do this.

Note: The following code block is not interactive.

For this course you don’t have to learn the syntax to create classes. Just notice that the class itself is defined first, and then indented beneath it are the attributes and methods. This is what it means when an attribute or method “belongs to a class.” Attributes and methods are defined in the code for that class. 

A class is like a blueprint for all things that share characteristics and behaviors. In this case, the class is Spaceship. There can be all different kinds of spaceships. They can have different names and different purposes. Whenever you create an object of a given class, you’re creating an instance of that class. This is also known as instantiating the class. In the code above, every time you instantiate an object of the Spaceship class it will start with its tractor beam set to off. The tractor beam is a class attribute. All instances of the Spaceship class have one. There are also instance attributes. These are attributes that you can assign when you instantiate the object. 

The next block of code uses the warp() method to set the warp speed to seven. Then it checks the current speed of the ship using the speed attribute.

This final block of code uses the tractor() method to toggle the tractor beam. Then it checks the current status of the tractor beam using the tractor_beam attribute.

This is just a basic example meant to demonstrate some of the fundamental ways that classes, attributes, and methods work and how they relate to each other, but classes can be very complex and have many attributes and methods. Depending on the work you’re doing as a data professional, knowledge about object-oriented programming will be helpful as you define your own classes, attributes, and methods to investigate the patterns, relationships, and meaning data holds. 

Key takeaways

Classes comprise the core objects of Python, which is why Python is known as an object-oriented language. Class objects are powerful because they contain unique tools designed specifically for that class packaged within them. Methods are functions that belong to a class; they perform actions or operations, and they use parentheses. Attributes are values or characteristics associated with a class or class instance; they do not use parentheses. And, while there are many classes, attributes, and methods pre-built into Python, there is a high level of customization offered in the object-oriented programming schema.

Comments

Popular posts from this blog

Life

Life has been bestowed upon us by the almighty and we all must value it. We should be thankful for all that we have and try to improve ourselves each day to build a better life. Technically, life is associated with feelings, growth and evolution. Like the plants have life because they grow; humans and animals have life as they feel sadness, happiness and they too grow. The journey of life may not always be smooth but we must keep going and stay positive all the times. Life is the most precious asset on this planet and must be protected irrespective of its form and appearance. Every species, not only humans, have a fundamental right to live their life, I whatever way they desire. Life is a gift of God to humanity and any attempt to disrupt or damage it will have undesirable consequences.

The nature and meaning of law

The nature and meaning of law has been described by various jurists. However, there is no unanimity of opinion regarding the true nature and meaning of law. The reason for lack of unanimity on the subject is that the subject has been viewed and dealt with by different jurists so as to formulate a general theory of legal order at different times and from different points of view, that is to say, from the point of view of nature, source, function and purpose of law, to meet the needs of some given period of legal development. Therefore, it is not practicable to give a precise and definite meaning to law which may hold good for all times to come. However, it is desirable to refer to some of the definitions given by different jurists so as to clarify and amplify the term ‗law‘. The various definitions of law propounded by legal theorists serve to emphasize the different facets of law and build up a complete and rounded picture of the concept of law. Hereinafter we shall refer ...

Love and It's Type

By  Meredith Shirey, MS, LMFT As a therapist specializing in  relationship issues , I often see people in therapy existentially contemplating what  love  really means. Some wonder whether they truly love(d) their partner, or if their partner truly loves/loved them. These are complicated questions because they often require defining “true love.” This is, of course, a subjective venture; love may look and feel different to different people. But in considering such questions for yourself, it may be helpful to think about love on a couple of levels: ego love and authentic love (often referred to as “soul love”). Ego Love When we determine we have fallen in love with someone, this is often done based on  euphoric  feelings of infatuation. We think about the person constantly, craving connection with them both physically and emotionally. We want to know their thoughts, feelings, wants, and needs. We want to know about their past, be in their prese...