Chereads / LIFE OF HABECK / Chapter 3 - python

Chapter 3 - python

1. illustrate the use of method overriding explain with example

2. Illustrate data abstraction with proper example

3. State use of namespace in python

4.what is local and globle variables

5. List built in class attributes

6. What is object give example

7. List reading keyboard input function

1. illustrate the use of method overriding explain with example

>

-

when you have two methods with the same name that each perform different tasks.

-

the child class has access to the properties and functions of the parent class method while also extending additional functions of its own to the method

The child class should have the same name and the same number of parameters as the parent class.

class Animal:

def Walk(self):

print('Hello, I am the parent class')

class Dog(Animal):

def Walk(self):

print('Hello, I am the child class')

print('The method Walk here is overridden in the code')

r = Dog()

r.Walk()

r = Animal()

r.Walk()

Output

The method Walk here is overridden in the code

Hello, I am the child class

Hello, I am the parent class

2. Illustrate data abstraction with proper example

>process of hiding the real implementation of an application from the user and emphasizing only on usage of it.

-

For example, consider you have bought a new electronic gadget. Along with the gadget, you get a user guide, instructing how to use the application, but this user guide has no info regarding the internal working of the gadget.

3. State use of namespace in python

>

1. State the use of namespace in python

-The namespace helps the Python interpreter to understand what exact method or variable is trying to point out in the code.

-A namespace is a system that has a unique name for each and every object in Python

-Python itself maintains a namespace in the form of a Python dictionary

-the role of a namespace is like a surname

-One might not find a single "Alice" in the class there might be multiple "Alice" but when you particularly ask for "Alice Lee" or "Alice Clark"

-Namespace is a way to implement scope. In Python, each package, module, class, function and method function owns a "namespace" in which variable names are resolved

2. What is local ๐Ÿ˜‡and global๐Ÿ˜ˆ variable

-๐Ÿ˜‡A variable declared inside the function is called local function".

-๐Ÿ˜ˆGlobal variables are those which are not defined inside any function and have a global scope

3.list buit-in class attributes in python

-Every Python class keeps following built-in attributes and they can be accessed using dot operator like any other attribute โˆ’

1.__dict__

โˆ’ Dictionary containing the class's namespace.

2.__doc__

โˆ’ Class documentation string or none, if undefined.

3.__name__

โˆ’ Class name.

4.__module__

โˆ’ Module name in which the class is defined. This attribute is "__main__" in interactive mode.

5.__bases__

โˆ’ A possibly empty tuple containing the base classes, in the order of their occurrence in the base class list.

6. What is object give example

>

collection of data (variables) and methods (functions) that act on those data. Similarly, a class is a blueprint for that object.

We can think of a class as a sketch (prototype) of a house. It contains all the details about the floors, doors, windows, etc. Based on these descriptions we build the house. House is the object.

7. List reading keyboard input function

>

two inbuilt functions to read the input from the keyboard

input ( prompt )

raw_input ( prompt )

scan(),

readline(),

print().