Monday, July 25, 2011

පහත දැක්වෙන පයිතන් ක‍්‍රමලේඛය සලකා බලන්න:

පහත දැක්වෙන පයිතන් ක‍්‍රමලේඛය සලකා බලන්න:

from date import date
class dtcheck:
     self.dob=date(y,m,d) - self.dob

def printage(self):
      return "your age %d" % ((self.age.days)/365)

මෙහි දැක්වෙන්නේ උපන් දිනය ලබාදීමෙන් පසු වයස අවුරුදුවලින් ප‍්‍රතිපානය කිරීම සඳහා සැකසූ පන්ති මොඩියුලයකි. මෙම මොඩියුලය "" යන ලිපිගොනු නාමයෙන් පයිතන් ක‍්‍රමලේඛයක් ලෙස සුරැකී ඇත. මෙම පන්ති මොඩියුලයෙන් වස්තුවක් (object) නිර්මාණය කර, එයට ආදානය ලබාදෙමින් ප‍්‍රතිදානය ලබාගැනීමට භාවිත කළ හැකි නිවැරදිථ පයිතන් මොඩියුලය කුමක් ද?

import dcheck
print w

import dcheck
print result

import dcheck
print u.printage()

import dcheck
print result

print result 

Python 3.x code

from datetime import date

class dtcheck:
    def __init__(self,y,m,d):
        self.dob=date(y,m,d) - self.dob
    def printage(self):
        return "your age %d"%((self.age.days)/365)

import dcheck
print (u.printage())


The import Statement:

You can use any Python source file as a module by executing an import statement in some other Python source file. import has the following syntax:
import module1[, module2[,... moduleN]

# Import module hello
import hello # Now you can call defined function


Hello : Dinuka

The from...import * Statement:

It is also possible to import all names from a module

from modname import *

The PYTHONPATH Variable:

The PYTHONPATH is an environment variable, consist list of directories.

set PYTHONPATH=c:\python20\lib;
UNIX system:
set PYTHONPATH=/usr/local/lib/python



Namespaces and Scoping:

Variables are names map to objects.

A namespace is a dictionary of variable names (keys) and their corresponding objects (values).

Python can access variables in a local namespace and in the global namespace. I

if a local and a global variable have the same name, the local variable shadows the global variable.

Each function has its own local namespace.

Python makes guesses on whether variables are local or global.

It assumes that any variable assigned a value in a function is local.

To assign a value to a global variable within a function,

use global VarName which tells Python that VarName is a global variable.


Honey = 2000
def AddHoney():
   # Uncomment the following line to fix the code:
   # global Honey
   Honey = Honey + 1

print Honey
print Honey

 what happens ? if  # global Honey uncommented like
global Honey

The Global variable get updated

Packages in Python:

A package is a hierarchical file directory structure that defines a single Python application environment that consists of modules and subpackages and sub-subpackages,


Consider a file available in Phone directory. This file has following line of source code:

def Mobs():
   print "I'm a mobile phone"
Similar way we have another two files having different functions with the same name as above:
  • Phone/ file having function Isdn()
  • Phone/ file having function G3()
Now create one more file in Phone directory :
  • Phone/
To make all of your functions available when you've imported Phone, you need to put explicit import statements in as follows:
from Pots import Pots
from Isdn import Isdn
from G3 import G3
After you've added these lines to, you have all of these classes available when you've imported the Phone package:


# Now import your Phone Package.
import Phone

This would produce following result:
I'm a mobile phone
I'm 3G Phone
I'm ISDN Phone

No comments:

Post a Comment