python 笔记

来源:互联网 发布:怎么统计淘宝分类消费 编辑:程序博客网 时间:2024/05/16 15:14

python 字符串方法

str.startwhth('str')

'str1' in str2

str.find('war')!=-1  #给定一个字符串在另外一个字符串中的位置,或者返回-1找不到指定支付字符串,str类也有

一个作为分隔符的字符串join 序列的整洁的方法


delimiter='_*_'

mylist=['Brazli','Russia','India',‘China’]

delimiter.join(mylist)   #Brazil_*_Russia_*_India_*_China 


import os

import  time

source=['/home/swaroop/byte','/home/swaroop/bin']

taeget_dir='/home/bak‘’

target=target_dir+time.strftime('%Y%m%d%H%M%S')+.zip

zip_command="zip -qr '%s' %s"%(target,' '.join(source))

os.system(zip_command)==0


if not os.pathn.exists(today)

os.makedir(today)

tar = 'tar -cvzf %s %s -X /home/swaroop/excludes.txt' % (target, ' '.join(srcdir))

-c 创建一个归档

-v 交互,命令更具交互性

-z 使用gzip过滤器

-f 强制创建归档,即如果已经有一个同名文件,他会被替换

-X 指定文件列表中的文件会被排除在备份文件之外, 可以在文件中指定*~不让备份包括所有以~结尾文件


类的方法与普通的函数只有一个特别的区别--他们必须有一个屋外的第一个参数名称,

但是在调用这个方法的时候不为这个参数赋值


class Person:

pass            #空白块

p=Person() 

print p


class Person:

def sayHi(self):

print 'Hello'

p=Person()

p.sayHi()


__init__方法

class Person:

def __init__(self,name):   #__init__发放定义为去一个参数name (以及普通的参数self)

self.name=name     

def sayHi(self):   

print 'Hello'

p=Person('Swaroop')

p.sayHi()



类与对象的方法

有两种类型的域--类的变量和对象的变量,他们是根据是类还是对此昂拥有这个变量的区分

累哦变量由一个类的所有对象(实例)共享使用。只有一个变量的拷贝,所以当某个对象对垒的变量

作了改动的时候,这个改动会反应到所有其他的实例上

对象的变量,有类的每个对象/实例拥有。因此每个对象有自己对应的这个域的拷贝,即他们是不共享的

class Person:

population=0

def __init__(self,name):  # __init__ 方法用一个名字来处事话Person 实例 这个方法让

population增加1,self.name 的值根据每个对象指定,这表明了他作为对此昂的本质

self.name=name

print '(Initializing %s)'%self.name

Person.population+=1


def   __del__(self)    #__del__ 方法,它的对象消逝的时候被调用。对象消逝

即对象不再被使用,占用的内存将返回给系统做它用

print '%s says byr '%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 dayHi(self):

print 'Hi%s'%self.name

def howMany(self):

if Person.population==1

print 'I am the only person here'

else:

print 'We %d '%Person.population

swaroop=Person('swaroop')

swaroop.sayHi()      #hi my name is swaroop 

swaroop.howMany() #I am the only person here 


kalam=Person('kaklam')

kalam.sayHi() #hi my name is kalam

kalam.howMany() #we have 2 person here 


swaroop.sayHi() #Hi my name is swaroop

swaroop.howMant() #we have 2 persons here 


#kalam says bye 

#There are still 1people left

#swaroop says bye 

#I am the last one