March 2009


There are loads of content and age old story telling way of explain classes , I take it the nerdy way and explain on the technical part of it , in this post .

Defining a class

class ClassName:   {statement-1}   .   .   .   {statement-N}

The statements can be simple statements or it can be methods .Normally the contain methods.

Class Objects:

These supports ===> attribute references and instantiation

To understand , those two lets consider a simple class definition :

class YouNity:    "My first class"    bond = 007    def hi(self):        return 'hello world'

Attribute reference ==> YouNity.bond or YouNity.hi()

Instantiation ===> all = YouNity () , creates a new instance of the class and assigns this object to the local variable all .Hence , we can access varibales and methods of the class , as all.bond or all.hi()

__init__() ans self

This method is very much similar to the Java constructor , lets see how it can be used ?!

>>> class Who:...     def __init__(self, first, last):...         self.firstName = first...         self.lastName = last... >>> name = Who("hemanth", "hm")>>> name.first,name.last(hemanth,hm)

In the above example , the __init__ method is called when the class is instantiated , the first argumentto the method is called "self" , the name self has absolutely no special meaning to Python , but can beunderstood as the current variables reference.

Inheritance


The child must follow the parent class , that is Derived followed by Base class in the declaration.

class DerivedClassName(BaseClassName):   {statement-1}   .   .   .   {statement-N}

If the Base is in some other package then ? 

class DerivedClassName(modname.BaseClassName):

Multiple parents to a single child !!!

class DerivedClassName(Base1, Base2, Base3):   {statement-1}   .   .   .   {statement-N}






There are loads of content and age old story telling way of explain classes , I take it the nerdy way and explain on the technical part of it , in this post .

Defining a class

class ClassName:   {statement-1}   .   .   .   {statement-N}

The statements can be simple statements or it can be methods .Normally the contain methods.

Class Objects:

These supports ===> attribute references and instantiation

To understand , those two lets consider a simple class definition :

class YouNity:    "My first class"    bond = 007    def hi(self):        return 'hello world'

Attribute reference ==> YouNity.bond or YouNity.hi()

Instantiation ===> all = YouNity () , creates a new instance of the class and assigns this object to the local variable all .Hence , we can access varibales and methods of the class , as all.bond or all.hi()

__init__() ans self

This method is very much similar to the Java constructor , lets see how it can be used ?!

>>> class Who:...     def __init__(self, first, last):...         self.firstName = first...         self.lastName = last... >>> name = Who("hemanth", "hm")>>> name.first,name.last(hemanth,hm)

In the above example , the __init__ method is called when the class is instantiated , the first argumentto the method is called "self" , the name self has absolutely no special meaning to Python , but can beunderstood as the current variables reference.

Inheritance


The child must follow the parent class , that is Derived followed by Base class in the declaration.

class DerivedClassName(BaseClassName):   {statement-1}   .   .   .   {statement-N}

If the Base is in some other package then ? 

class DerivedClassName(modname.BaseClassName):

Multiple parents to a single child !!!

class DerivedClassName(Base1, Base2, Base3):   {statement-1}   .   .   .   {statement-N}






Reading or Writing to an I/O device is very much easier than reading Human mind!

The first on amongst the many functions in Python which helps in I/O is raw_input()

raw_input(); the name itself suggests , the input is in the raw format , that is it’s simply returns the string read from the user {ending with new line char}.
An argument to it , is displayed as a prompt before the user enters the data.

Ex :

print raw_input(‘What is your name?’)

o/p : What is your name ? { waits until user types and hits enter }

Next up is input() , it uses raw_input to read a string of data, and then attempts to evaluate the same and return the value of evaluation.

Ex:

x = input(‘Give me 5 random numbers please’)
if the user enters 1 2 3 4 5 , the input function will return me a list {1,2,3,4,5} !!

File Input

File can be handled very easily in python , the following example makes it clear.

for
line in open(‘try.txt’, ‘r’):

print
line[0]

Two lines to open read and print the lines in the file , on to the console!!

Line one is to open the file , it can be ‘r’, ‘w’, or ‘rw’, or any combination of them and the read data is stored in the line variable and is looped till EOF .

Line two as simple as it is , printing the data read in the loop.

P.S : The looping can be done in other was also


__stdin__, __stdout__, and __stderr__ are standard file objected defined in sys module .

The simple example on how to use sys module is shown below :

import sysfor line in sys.stdin:print line

This reads line form user and prints it on the stdout , it's read from stdin.Different functions in the sys library can be found in : sysSimilarly sys.stdout.write can be used to write

import syswrite = sys.stdout.writewrite('h')write('e')write('m')write('a')write('n')

the o/p will be : heman

This is just a simple post , an glimpse on the giant mountain called I/O in python.

Reading or Writing to an I/O device is very much easier than reading Human mind!

The first on amongst the many functions in Python which helps in I/O is raw_input()

raw_input(); the name itself suggests , the input is in the raw format , that is it’s simply returns the string read from the user {ending with new line char}.
An argument to it , is displayed as a prompt before the user enters the data.

Ex :

print raw_input(‘What is your name?’)

o/p : What is your name ? { waits until user types and hits enter }

Next up is input() , it uses raw_input to read a string of data, and then attempts to evaluate the same and return the value of evaluation.

Ex:

x = input(‘Give me 5 random numbers please’)
if the user enters 1 2 3 4 5 , the input function will return me a list {1,2,3,4,5} !!

File Input

File can be handled very easily in python , the following example makes it clear.

for
line in open(‘try.txt’, ‘r’):

print
line[0]

Two lines to open read and print the lines in the file , on to the console!!

Line one is to open the file , it can be ‘r’, ‘w’, or ‘rw’, or any combination of them and the read data is stored in the line variable and is looped till EOF .

Line two as simple as it is , printing the data read in the loop.

P.S : The looping can be done in other was also


__stdin__, __stdout__, and __stderr__ are standard file objected defined in sys module .

The simple example on how to use sys module is shown below :

import sysfor line in sys.stdin:print line

This reads line form user and prints it on the stdout , it's read from stdin.Different functions in the sys library can be found in : sysSimilarly sys.stdout.write can be used to write

import syswrite = sys.stdout.writewrite('h')write('e')write('m')write('a')write('n')

the o/p will be : heman

This is just a simple post , an glimpse on the giant mountain called I/O in python.