Giter Club home page Giter Club logo

student_data_encapsulation's Introduction

Encapsulation

A concept related to the object oriented approach of programming.

Encapsulation is the packing of data and functions operating on that data into a single component and restricting the access to some of the object's components. It essentially means the internal representation of an object is generally hidden from view outside of the objects definition.

A good example is a class, it encapsulates all the data that its member methods and attributes have.

devops_student [Parent/ Base Class]

  • Attributes
    • current_grade private
    • current_trainer public
  • Methods
    • print_details public
    • change_current_grade private

student_data [Child/ Derived Class]

  • Methods
    • public_method public
    • private_method private

Some would say that Encapsulation is very similar to Abstraction, however there is one major difference :

  • Encapsulation = Information hiding
  • Abstraction = Implementation hiding

To declare a member as protected you use a _ by convention. This tells others :

" Don't touch this, unless you are a subclass of this class ".

To declare a member as private you use two __ in front of it to hide it from users when accessing them from outside the class.

If you change the value of a private attribute it will actually take place but you are expected to not do it if it has been distinguished as a private attribute. A proper way to do this is to add a getter and setter property function that allows proper alterations of private attributes inside a class.

    # Setter method
    def setcurrentgrade(self, current_grade):
        print("set current_grade() called")
        self.__current_grade = current_grade

    # Getter method
    def getcurrentgrade(self):
        print("get current_grade() called")
        return self.__current_grade

    current_grade = property(getcurrentgrade, setcurrentgrade)

Trying to access private attributes and methods of a class will return an attribute error usually, this is because python essentially hides a variable to everyone outside the class by performing a technique called name mangling, this changes the name of a variable. This is not 100% secure though as you can still access the attributes and methods by entering the altered name which looks like this:

object._Class__private_method

object._Class__private_attribute

student_data_encapsulation's People

Contributors

johnbyrnejames avatar

Watchers

James Cloos 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.