Python_day7

  • sys模块
import sys

def _add(a, b):
	return a + b

def _sub(a, b):
	return a - b

def _mul(a, b):
	return a * b

def _div(a, b):
	return a / b

def caculate(num1, num2, op):
	'''四则运算'''
	'''
	op_ls = ['+', '-', 'x', '/']
	fun_ls = [_add, _sub, _mul, _div]	
	for i in range(len(op_ls)):
		if op_ls[i] == op:
			return (fun_ls[i])(num1, num2)
	'''
	op_fun = {'+':_add, '-':_sub, 'x':_mul
  • time模块
import time

# 时间戳
tm = time.time()
# 时间结构
ltime = time.localtime()
print(ltime.tm_year)
# 时间字符串
str_time = time.strftime("%Y-%m-%d %H:%M:%S", ltime)
print(str_time)
  •   验证模块的使用
__author__='zhangzongyan'

# import pag.moduler as pm
# from pag.moduler import _test
from pag.moduler import * # 私有函数(_或__开头的函数)没导入
import hw
import sys

if __name__ == '__main__':
	print(__name__)
	print(__doc__)
	print(__author__)

	# 得到模块的搜索路径
	print(sys.path)

	# pm._test()
	test()

 导用模块的目录下应:touch __init__.py 

import main

print(main.__name__)
print(main.__doc__)
print(main.__author__)
  • 面向对象(oop):

  类:抽象概念,类型
  对象:实际物体,类实例化对象
  属性:
  描述类---》类属性
  描述对象---》实例属性

# 面向过程描述学生的成绩
d = {'miguitian':80, 'yangzhichao':88, 'zhangxue':100, 'liuhongsheng':12}

# 抽象类型
class Student(object):
    count = 0 # 类属性:类名.属性名
    def __init__(self, score): # --->构造函数:实例化对象时自动调用的
        # print('__init__ is called')
        # self : 当前对象
        self.score = score
        Student.count += 1

    # 实例方法
    def setName(self, name):
        if 1 < len(name) < 32:
            self.name = name
            return True
        else:
            return False

    def run(self):
        print('%s is running' % self.name)

    def __del__(self): # 析构方法:对象销毁的时候自动调用调用
        print('delete.....')

# 实例化对象
s1 = Student(100)
# 访问对象的属性
print(s1.score)
s1.name = 'chenyunliang'
print(s1.name)

del s1

s2 = Student(98)
print(s2.score)

# 调用方法
s2.setName('python')
print(s2.name)

s2.run()

print('学生对象有%d个'%Student.count)
# 抽象类型
class Student(object):
    count = 0 # 类属性:类名.属性名
    def __init__(self, score): # --->构造函数:实例化对象时自动调用的
        self.__score = score # 私有属性,只允许在本类中访问
        Student.count += 1

    # 实例方法
    def setName(self, name):
        if 1 < len(name) < 32:
            self.name = name
            return True
        else:
            return False

    def run(self):
        print('%s is running' % self.name)

    def getScore(self):
        self.__privateFun()
        return self.__score

    # 私有方法
    def __privateFun(self):
        print('private....')

    def __del__(self): # 析构方法:对象销毁的时候自动调用调用
        print('delete.....')

# 实例化对象
s1 = Student(100)
# 访问对象的属性
print(s1.getScore())

# 私有属性---》解释器做了名字的修改
print(s1._Student__score)

# 私有方法
# s1.__privateFun()
s1._Student__privateFun()
  • 继承
class Animal(object):
    def __init__(self, name, age=1, color='white'): # 重写
        self.name = name
        self.age = age
        self.__color = color  # _Animal__color

    def show(self):
        print(self.name, self.age, self.__color)


class Dog(Animal):
    def __init__(self, name, age, breed):
        # 调用父类方法
        # Animal.__init__(self, name, age)
        # super(Dog, self).__init__(name, age)
        super().__init__(nam

猜你喜欢

转载自www.cnblogs.com/ZHang-/p/10115883.html