Python development skills of a small circle ape?

python is now a mainstream development language, more and more people begin to learn python, python actually have a lot of learning to learn skills, today's small circle ape teacher with you to find out: python learning skills, so you can easily and quickly grasp the python, the following Let's begin our journey python!

Interface to the external display is limited:

When publishing python third-party package, do not want to code all of the function or class may be external import, add __all__ property in __init__.py in the list, fill in the class or function name can import, you can play import restriction effect of preventing external import functions or other classes.

#!/usr/bin/envpython

#-*-coding:utf-8-*-

from base import  APIBase

from client import Client

from  decorator  import interface,export,stream

from server import Server

from storage  import Storage

from util import(LogFormatter,disable_logging_to_stderr,

enable_logging_to_kids,info)

__all__=['APIBase','Client','LogFormatter','Server',

'Storage','disable_logging_to_stderr','enable_logging_to_kids',

'export','info','interface','stream']

filter usage:

Relative filter, it will be more frequent map and reduce the use of some, as the name filter, to filter out some of the elements according to certain rules.

#!/usr/bin/envpython

#-*-coding:utf-8-*-

lst=[1,2,3,4,5,6]

#所有奇数都会返回True,偶数会返回False被过滤掉

print filter(lambdax:x%2!=0,lst)

#输出结果

[1,3,5]

一行作判断:

当条件满足时,返回的为等号后面的变量,否则返回else后语句。

lst=[1,2,3]

new_lst=lst[0]iflstisnotNoneelseNone

print new_lst

#打印结果

1

装饰器之单例:

使用装饰器实现简单的单例模式

#单例装饰器

def singleton(cls):

instances=dict()#初始为空

def_singleton(*args,**kwargs):

if clsnotininstances:#如果不存在,则创建并放入字典

instances[cls]=cls(*args,**kwargs)

returninstances[cls]

return_singleton

@singleton

classTest(object):

pass

if__name__=='__main__':

t1=Test()

t2=Test()

#两者具有相同的地址

printt1,t2

    These are the small apes circle python lecturers to share the Python development skills, I hope you can look after master this skill, simple and efficient learning python, want to learn more little friends can go to a small circle to learn more ape skills, want to learn Python friends on it.

Guess you like

Origin blog.csdn.net/qq_42210792/article/details/91366175