from date import date
class dtcheck:
def_init_(self,y,m,d):
self.now=date.today()
self.dob=date(y,m,d)
self.age=self.now - self.dob
def printage(self):
return "your age %d" % ((self.age.days)/365)
මෙහි දැක්වෙන්නේ උපන් දිනය ලබාදීමෙන් පසු වයස අවුරුදුවලින් ප්රතිපානය කිරීම සඳහා සැකසූ පන්ති මොඩියුලයකි. මෙම මොඩියුලය "dcheck.py" යන ලිපිගොනු නාමයෙන් පයිතන් ක්රමලේඛයක් ලෙස සුරැකී ඇත. මෙම පන්ති මොඩියුලයෙන් වස්තුවක් (object) නිර්මාණය කර, එයට ආදානය ලබාදෙමින් ප්රතිදානය ලබාගැනීමට භාවිත කළ හැකි නිවැරදිථ පයිතන් මොඩියුලය කුමක් ද?
import dcheck
w=dcheck.dtcheck(1976,11,10)
result=w.printage()
print w
import dcheck
w.dcheck(1976/11/10)
result=w.printage()
print result
import dcheck
u=dcheck.dtcheck(1976,11,10)
print u.printage()
u=dcheck.dtcheck(1976,11,10)
print u.printage()
import dcheck
w.dtcheck.dcheck(1976,11,10)
result=w.printage()
print result
w=dcheck.dcheck(1976/11/10)
result=w.printage()
print result
Python 3.x code
from datetime import date
class dtcheck:
def __init__(self,y,m,d):
self.now=date.today()
self.dob=date(y,m,d)
self.age=self.now - self.dob
def printage(self):
return "your age %d"%((self.age.days)/365)
import dcheck
u=dcheck.dtcheck(1976,11,10)
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.print_func("Dinuka")
Hello : Dinuka
The from...import * Statement:
It is also possible to import all names from a modulefrom modname import * |
The PYTHONPATH Variable:
The PYTHONPATH is an environment variable, consist list of directories.set PYTHONPATH=c:\python20\lib; |
set PYTHONPATH=/usr/local/lib/python |
#!/usr/bin/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.
#!/usr/bin/python Honey = 2000 def AddHoney(): # Uncomment the following line to fix the code: # global Honey Honey = Honey + 1 print Honey AddHoney() 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,Example:
Consider a file Mobs.py available in Phone directory. This file has following line of source code:#!/usr/bin/python def Mobs(): print "I'm a mobile phone" |
- Phone/Isdn.py file having function Isdn()
- Phone/G3.py file having function G3()
- Phone/__init__.py
from Pots import Pots from Isdn import Isdn from G3 import G3 |
#!/usr/bin/python # Now import your Phone Package. import Phone Phone.Mobs() Phone.Isdn() Phone.G3() |
I'm a mobile phone I'm 3G Phone I'm ISDN Phone |
No comments:
Post a Comment