python基础(一):python语法一览

来源:互联网 发布:淘宝店铺二级域名收费 编辑:程序博客网 时间:2024/06/05 01:52

python语法一览

# mymodule.pyfrom package_name import modulefrom module import global_variablefrom module import class# it is discouraged, it will import all into current module namespacefrom module import *  import module# use as to rename in current scope# support follow three ways of from expressfrom module import class as newclassnamefrom package import module as newmodulename from module import global_variable as newvar# not supportfrom module import method # global variable, variable scope is current module# they can be accessed by mymodule.global_var1, mymodule.global_var2global_var1 =1global_var2 = None# start with '_' can be stop to be imported by from module import *, but can be import by : from mymodule import  _global_var3_global_var3 = 0# global methoddef global_method():    # when need use global variable, you should use 'global' keyword    global global_var1    print('i am global static method')# control flowif (something) and (something1) or (something11) or ( not something111()):    do_something()elif something2 = 2 or something2 != 3:    do_something2()else:    do_someelse()# support while, but not support do...whilewhile(something):    do()else:   # this is optional     do_else()for var in collection:    do1    # suporrt break, continue# with with Myclass:    dosomething()with Myclass() as my:    my.dosomething()# define a class# class suport multi-Hirentclass Myclass(Thread,Request):    # private static property, python interpreter will rename this as _Myclass__private_static_property, don't access this property by this way, this will be different name in different python version    __private_static_property = None    # public static property, this can be accessed by Myclass.public_static_property1    # public_static_property1 = None    # constructor, can define parameter freedom , python doesn't support overload constructor, but can simulate by __new__ with different parameter    def __init__(self, *args, **kwargs):        # call super class's constructor        Thread.__init__(args, kwargs)        # call super class's constructor        Request.__init__(args, kwargs)        # add instance property        self.instance_propert1 = None        self.instance_propert2 = None    # 1. execute before __init__    # 2. must return a instance of current class    # 3. instance will transfer to __init__ as self parameter    def __new__(cls, *args, **kwargs):        # class private static variable        cls.__private_static_propert2 = None        # class public static variable, this way is the same as public_static_property1        cls.public_static_property2 = None        # must return a instance of current class        return Myclass(args,kwargs)    # enter method    def __enter__(self):        print(' iam enter')    # destroy method, will auto call by python intepreter    # no args    def __destroy__(self):        print('destory object')    # exit method    def __exit__(self):        print('i am exit')    #public instance method    def get_property():        return self.instance_property2    # private instance method    def __count():        self.instance_property1 += 1    # override super class's method    def run():        print('i am override method')    # overload method    def run(arg):        print('i am overload method')# 1. when excute module, this variable '__name__' will be current module name# 2. the main module(python program startup module) will be special, it will be '__main__', if __name__ == '__main__':        test()
0 0
原创粉丝点击