Chinaunix首页 | 论坛 | 博客
  • 博客访问: 74918
  • 博文数量: 12
  • 博客积分: 1425
  • 博客等级: 上尉
  • 技术积分: 220
  • 用 户 组: 普通用户
  • 注册时间: 2006-12-11 09:15
文章分类

全部博文(12)

文章存档

2011年(1)

2010年(3)

2008年(8)

我的朋友

分类: Python/Ruby

2008-09-26 18:36:21

第10章
备份的python脚本例子:
使用了os和time模块
使用time.strftime()函数获得时间
使用os.system函数 运行 命令,如果命令成功运行,它返回0,否则它返回错误号。
使用os.exists函数检验在主备份目录中是否有以当前日期作为名称的目录。如果没有,我们使用os.mkdir函数创建。
os.sep变量的用法——这会根据你的操作系统给出目录分隔符,即在Linux、Unix下它是'/',在Windows下它是'\\'
偶将rar.exe所有的路径添加了path系统变量中了。
使用raw_input函数得到用户的注释,然后通过len函数找出输入的长度以检验用户是否确实输入了什么东西。
#!/usr/bin/python
#Filename:backup_ver2.py
import os
import time
source = ['E:\\Favorites','E:\\EZoo']
target_dir = 'E:\\backup'
today = target_dir+time.strftime('%Y%m%d')
now = time.strftime('%H%M%S')
comment = raw_input('Enter a comment -->')
if len(comment) ==0:
    target = today + os.sep + now
else:
    target = today + os.sep + now + '_'\
    + comment.replace(' ','_')
   
if not os.path.exists(today):
    os.mkdir(today)
    print 'Successfully created directory',today
   
rar_command = 'rar A "%s" %s' % (target,' '.join(source))
print rar_command
if os.system(rar_command) == 0:
    print 'Sucessful backup to',target
else:
    print 'Backup Failed'
 
17:35 2008-9-26
第11章 面向对象的编程
类创建一个新类型,而对象这个类的 实例。
属于一个对象或类的变量被称为域。
对象也可以使用 属于 类的函数来具有功能。这样的函数被称为类的方法。
域和方法可以合称为类的属性。
域有两种类型——属于每个实例/类的对象或属于类本身。它们分别被称为实例变量和类变量。
类使用class关键字创建。类的域和方法被列在一个缩进块中。
1、self
Python中的self等价于C++中的self指针和Java、C#中的this参考。
假如你有一个类称为MyClass和这个类的一个实例MyObject。当你调用这个对象的方法MyObject.method(arg1, arg2)的时候,这会由Python自动转为MyClass.method(MyObject, arg1, arg2)——这就是self的原理了。
2、类
class Person:
    pass # An empty block
p = Person()
print p
输出为:
E:\02_study\21-python\prac_code>python simplestclass.py
<__main__.Person instance at 0x009F2080>
注:
它告诉我们我们已经在__main__模块中有了一个Person类的实例。
0x009F2080为存储到计算机内存的地址。
3、对象的方法
类/对象可以拥有像函数一样的方法,这些方法与函数的区别只是一个额外的self变量。
class Person:
    def sayHi(self):
        print 'Hello, how are you?'
p = Person()
p.sayHi()
注意:sayHi方法没有任何参数,但仍然在函数定义时有self
4、__init__方法
在类的一个对象被建立时,马上运行,可以用来对你的对象做一些你希望的 初始化 。注意,这个名称的开始和结尾都是双下划线
class Person:
    def sayHi(self):
        print 'Hello, how are you?',self.name
    def __init__(self, name):
        self.name = name
p = Person('Swaroop')
p.sayHi()
5、类与对象的方法
class Person:
    '''Represents a person.'''
    population = 0
    def __init__(self, name):
        '''Initializes the person's data.'''
        self.name = name
        print '(Initializing %s)' % self.name
        # When this person is created, he/she
        # adds to the population
        Person.population += 1
    def __del__(self):
        '''I am dying.'''
        print '%s says bye.' % self.name
        Person.population -= 1
        if Person.population == 0:
            print 'I am the last one.'
        else:
            print 'There are still %d people left.' % Person.population
    def sayHi(self):
        '''Greeting by the person.
        Really, that's all it does.'''
        print 'Hi, my name is %s.' % self.name
    def howMany(self):
        '''Prints the current population.'''
        if Person.population == 1:
            print 'I am the only person here.'
        else:
            print 'We have %d persons here.' % Person.population
swaroop = Person('Swaroop')
swaroop.sayHi()
swaroop.howMany()
print '********************************'
kalam = Person('Abdul Kalam')
kalam.sayHi()
kalam.howMany()
print '********************************'
echo = Person('echo')
echo.sayHi()
echo.howMany()
print '********************************'
注:
(1)、docstring对于类和方法同样有用。我们可以在运行时使用Person.__doc__和Person.sayHi.__doc__来分别访问类与方法的文档字符串。
(2)、就如同__init__方法一样,还有一个特殊的方法__del__,它在对象消逝的时候被调用。对象消逝即对象不再被使用,它所占用的内存将返回给系统作它用。
(3)当对象不再被使用时,__del__方法运行,但是很难保证这个方法究竟在 什么时候 运行。如果你想要指明它的运行,你就得使用del语句。
(4)、Python中所有的类成员(包括数据成员)都是 公共的 ,所有的方法都是 有效的 。
只有一个例外:如果你使用的数据成员名称以 双下划线前缀 比如__privatevar,Python的名称管理体系会有效地把它作为私有变量。
这样就有一个惯例,如果某个变量只想在类或对象中使用,就应该以单下划线前缀。而其他的名称都将作为公共的,可以被其他类/对象使用。记住这只是一个惯例,并不是Python所要求的(与双下划线前缀不同)。
同样,注意__del__方法与 destructor 的概念类似。
 
6、继承
Python不会自动调用基本类的constructor,你得亲自专门调用它。
如果它不能在导出类中找到对应的方法,它才开始到基本类中逐个查找。
阅读(623) | 评论(0) | 转发(0) |
0

上一篇:python起步3

下一篇:Dive Into Python学习笔记1

给主人留下些什么吧!~~