Skip to content

arcesblas/OOP_python

Repository files navigation

Object Oriented Programming (OOP) in Python

Basic Concepts and Advantages of OOP

  • What is OOP?
    Object-oriented programming (OOP) is a computer programming model that organizes software design around data, or objects, rather than functions and logic. An object can be defined as a data field that has unique attributes and behavior. Source

  • Name the four basic principles of Object-Oriented Programming.
    Abstraction, inheritance, encapsulation, and polymorphism. Source

  • Why does OOP contribute to the modularity of a program?
    The software enables modularity by allowing developers to break down a complex problem into self-contained units to make specific designs. Source

Classes

  • What is a class?
    In object-oriented programming, a class is a blueprint for creating objects (a particular data structure), providing initial values for state (member variables or attributes), and implementations of behavior (member functions or methods). Source

  • What type of word is typically used as the name of a class?
    A noun

  • Explain the syntax used to define a class in Python.

Class Dog():
  
  def __init__(self, atributes):
      self.atributes = atributes

Instances and Instance Attributes

  • What is an instance?
    An instance is simply defined as a case or occurrence of anything. Source

  • How are classes related to instances?
    An instance of a class is an object. Source

  • What is an instance attribute?
    Instance attributes are attributes or properties attached to an instance of a class. Instance attributes are defined in the constructor. Source

  • What is the role of init () in the creation and initialization of instance attributes?
    The init function is called every time an object is created from a class. The init method lets the class initialize the object’s attributes and serves no other purpose. Source

  • What is the role of the keyword ’self’ in a Python class?
    This handy keyword allows you to access variables, attributes, and methods of a defined class in Python. Source

  • How to Define Nonpublic Methods in a Python Class?
    Strictly speaking, everything defined in a Python class is public. Source

  • Explain public, protected and private members
    Public Members: Public members (generally methods declared in a class) are accessible from outside the class. The object of the same class is required to invoke a public method.All members in a Python class are public by default.
    Protected Members: Protected members of a class are accessible from within the class and are also available to its sub-classes. No other environment is permitted access to it. This enables specific resources of the parent class to be inherited by the child class.
    Private Members: Python doesn't have any mechanism that effectively restricts access to any instance variable or method. Python prescribes a convention of prefixing the name of the variable/method with a single or double underscore to emulate the behavior of protected and private access specifiers. Source

Class Attributes

  • What is a class attribute?
    A class attribute is a Python variable that belongs to a class rather than a particular object. Source

  • Describe the differences between class attributes and instance attributes
    Class attributes are the variables defined directly in the class that are shared by all objects of the class. Instance attributes are attributes or properties attached to an instance of a class. Instance attributes are defined in the constructor. Source

Encapsulation and Abstraction

  • Describe the principle of Encapsulation and its importance in Object Oriented Programming.
    Encapsulation is a way to restrict the direct access to some components of an object, so users cannot access state values for all of the variables of a particular object. Source

  • Describe the principle of Abstraction and its importance in Object-Oriented Programming.
    Through the process of abstraction, a programmer hides all but the relevant data about an object in order to reduce complexity and increase efficiency. Source

  • Describe what happens during the process of Name Mangling and its main purpose.
    In name mangling process any identifier with two leading underscore and one trailing underscore is textually replaced with _classname__identifier where classname is the name of the current class. Source

Properties, Getters, and Setters

  • What is a getter?
    A method that allows you to access an attribute in a given class Source

  • What is a setter?
    A method that allows you to set or mutate the value of an attribute in a class. Source

  • What is a property in Python?
    Python's property() is the Pythonic way to avoid formal getter and setter methods in your code. This function allows you to turn class attributes into properties or managed attributes. Source

Aliasing, Mutation, and Cloning

  • What is aliasing?
    In Python, aliasing happens whenever one variable's value is assigned to another variable, because variables are just names that store references to values. Source

  • Define mutation
    In simple words, mutable means ‘able to be changed’ and immutable means ‘constant’. Source

  • What is cloning?
    If we want to modify a list and also keep a copy of the original, we need to be able to make a copy of the list itself, not just the reference. This process is sometimes called cloning, to avoid the ambiguity of the word copy. Source

About

Object Oriented Programming in Python

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages