Coder Social home page Coder Social logo

dsc-oop-recap's Introduction

OOP - Recap

Introduction

In this section, you learned about Object Oriented Programming (OOP) as a foundational practice for software development and programming.

Objectives

You will be able to:

  • Summarize the importance of OOP
  • Explain class attributes and methods
  • Explain self in classes
  • Explain the init method
  • Explain object initialization
  • Explain object inheritance

OOP Overview

In this section, you learned all about Object Orientated Programming and how to define classes. Like functions, using classes in your programming can save you a lot of time and repetitive tasks. Classes go further then functions in allowing you to also persist data. After all, class methods are fairly analogous to functions, while attributes add additional functionality, acting as data storage containers. Moreover, classes have additional flexibility through inheritance, allowing you to define various levels of abstraction.

Class Structures

As you saw, the most basic class definition starts off with:

class Class_name:

From there, you then saw how you can further define methods to the class:

class Class_name:
    def method1(self):
        pass #ideally a method does something, but you get the point
    def method2(self):
        print('This is a pretty useless second method.')

And from there, you learned more about self and the __init__ method. Recall that the __init__ method allows you to specify required and default parameters for your class instances. Furthermore, self then allows you to reference object attributes within class definitions. So for example, you might require a class to have a name and a number when instantiated:

class Class_name:
    def __init__(self, name, number):
        self.name = name
        self.number = number
    def update_name(self, new_name):
        self.name = new_name
    def update_number(self, new_number):
        self.number = new_number

You could also add default behavior, allowing the user to not specify a specify parameter when they instantiate an instance of the class:

class Class_name:
    def __init__(self, name=None, number=None):
        self.name = name
        self.number = number
    def update_name(self, new_name):
        self.name = new_name
    def update_number(self, new_number):
        self.number = new_number

Creating Instances

Recall that once you define a class, you can then create instances of that class to bring it to life and use it!

For example, you might import the LinearRegression class from scikit-learn in order to create a regression model!

Remember, creating an instance of a class would look like this:

from sklearn.linear_model import LinearRegression() #importing our class definition
reg = LinearRegression() #creating an instance of the class

Once you create an instance object of the class, you can then use all the methods associated with that class!

Object Inheritance

Finally, you examined object inheritance. You saw that you can create subclasses. To create a subclass of another class you simply reference the parent class when defining the child class as in:

class Child_class(Parent_class):

Finally, there was a brief mention of python's super() function. While it can be useful, some caution should be warranted here; the super() function is substantially different between python 2 and 3, and is not as straightforward as it appears. If you're a Java programmer, super() will be familiar to you in many aspects, but still works differently in python. You're not apt to need its functionality early on in your programming, but if you wish to learn more, start by checking out its documentation further here.

Summary

Congrats! You now have a solid introduction to OOP which will be fundamental for collaboration and writing concise modular code!

dsc-oop-recap's People

Contributors

loredirick avatar mas16 avatar mathymitchell avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.