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

python语法一览

# mymodule.py

from package_name import module
from module import global_variable
from module import class
# it is discouraged, it will import all into current module namespace
from module import *  

import module
# use as to rename in current scope
# support follow three ways of from express
from module import class as newclassname
from package import module as newmodulename 
from module import global_variable as newvar

# not support
from module import method 

# global variable, variable scope is current module
# they can be accessed by mymodule.global_var1, mymodule.global_var2
global_var1 =1
global_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 method
def global_method():
    # when need use global variable, you should use 'global' keyword
    global global_var1
    print('i am global static method')

# control flow
if (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...while
while(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-Hirent
class 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值