# Python

## What is First-Class Object?

Everything in Python is an object, including functions.

More specifically, we can assign functions to variables, store them in data structures, pass or return them to and from other functions.

First-class functions allow developers to abstract away and pass around behavior in their programs.

Functions can be nested and they can capture and carry some of the parent function's state with them. Functions that do this are called closures.

Object can be made callable and in many cases this allows developers to treat them like functions.

## What is Generator?

Generator functions are syntactic sugar for writing objects that support the iterator protocol.

Generator helps abstract away most of the boilerplate/template code needed for writing class-based iterators.

Generators look like functions but instead of using return, it useYield statements to pass back data to the caller.&#x20;

## Python v.s Java

|               Python               |                       Java                      |
| :--------------------------------: | :---------------------------------------------: |
|          dynamically typed         |                 statically typed                |
|               simply               |                     verbose                     |
|            Speed: slower           |                      faster                     |
|       Braces and Indentation       |                       semi                      |
|  Database Access Layers are weaker | <p>JDBC </p><p>(Java DataBase Connectivity)</p> |

### what is Dynamically typed?

&#x20;Its means that we don’t need to declare the types of variables, they are assumed at run-time.

### Python is more of simplicity.

Example: swap two variables.

### Python is a litter slower.

The types of variables are assumed at run-time so extra work are needed.

## Class Variables and Instance Variables

```python
Class Dog:
    legs = 4 # class variables
    def __init__(self, name):
        self.name = name
        # self.name instance variable

 if __name__ == '__main__':
    a = Dog('A')
    print(a.name) # A
    print(a.legs) # 4
    print(Dog.legs) # 4
    print(Dog.name) # Error
    
```

## What is Abstract Base Class?

&#x20;Abstract classes are classes that contain one or more abstract methods.&#x20;

An abstract method is a method that is declared, but contains no implementation.

Abstract base classes ensure that derived classes implement particular methods from the base class at instantiation time.

Using Abstract base classes can help avoid bugs and make class hierarchies easy to maintain

## What is Encapsulation?&#x20;

In an object oriented python program, you can _restrict access_ to methods and variables. This can prevent the data from being modified by accident and is known as _encapsulation._

```python
class Car:
 
    def __init__(self):
        self.__updateSoftware()
 
    def drive(self):
        print 'driving'
 
    def __updateSoftware(self):
        print 'updating software'
 
redcar = Car()
redcar.drive()
#redcar.__updateSoftware()  not accesible from object.
```

## What is Polymorphism?

&#x20;Sometimes an object comes in many types or forms. If we have a button, there are many different draw outputs (round button, check button, square button, button with image) but they do share the same logic: onClick().  We access them using the same method . This idea is called _Polymorphism_.

```python
class Bear(object):
    def sound(self):
        print "Groarrr"
 
class Dog(object):
    def sound(self):
        print "Woof woof!"
 
def makeSound(animalType):
    animalType.sound()
 
 
bearObj = Bear()
dogObj = Dog()
 
makeSound(bearObj)
makeSound(dogObj)
```

## &#x20;What is Inheritance?

Classes can inherit functionality of other classes. If an object is created using a class that inherits from a superclass, the object will contain the methods of both the class and the superclass. The same holds true for variables of both the superclass and the class that inherits from the super class.

## What is Overloading?

Given a single method or function, we can specify the number of parameters ourselves.

Depending on the function definition, it can be called with zero, one, two or more parameters.

This is known as _method overloading._

## What is overriding?

If there is any method in the superclass and a method with the same name in a subclass, then by executing the method, the method of the corresponding class will be executed.
