os.sep的功能是自动辨别操作系统,给出不同的分隔符,Windows上是,Linux上是/,原理是明白了,功能也很不错,但是作者的例子。只有一处使用了os.sep,其他的地方还是老的写法啊(E:)
8.
可以使用@修饰符声明一个类方法:
@classmethod
def howMany(klass):
'''Prints the current population.'''
print('We have {0:d} robots.'.format(Robot.population))
9.
可以将以个类用Metaclasses的方式声明为抽象类抽象方法
from abc import *
class SchoolMember(metaclass=ABCMeta):
'''Represents any school member.'''
def __init__(self, name, age):
self.name = name
self.age = age
print('(Initialized SchoolMember: {0})'.format(self.name))
@abstractmethod
def tell(self):
'''Tell my details.'''
print('Name:"{0}" Age:"{1}"'.format(self.name, self.age), end=" ")
#pass
10.
文件读写的模式又增加了两种:文本本件('t')二进制文件('b')。
11.将打开文件的操作放到使用with语句修饰的方法中,书上说好处是让我们更专注于文件操作,让代码看起来不凌乱,本文还不能完全体会with的好处。现给出示例代码供大家参考:
#!/usr/bin/python
# Filename: using_with.py
from contextlib import context
@contextmanager
def opened(filename, mode="r")
f = open(filename, mode)
try:
yield f
finally:
f.close()
with opened("poem.txt") as f:
for line in f:
print(line, end='')
12.python3.0中添加了logging module,给我的感觉类似于Java中的log4j,直接看代码:
import os, platform, logging
if platform.platform().startswith('Windows'):
logging_file = os.path.join(os.getenv('HOMEDRIVE'),
os.getenv('HOMEPATH'), 'test.log')
else:
logging_file = os.path.join(os.getenv('HOME'), 'test.log')
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s : %(levelname)s : %(message)s',
filename = logging_file,
filemode = 'w',
)
logging.debug("Start of the program")
logging.info("Doing something")
logging.warning("Dying now")
希望本文所述能对大家理解Python3.0与Python2.X一些区别性的用法有所帮助。










