uuid
import uuid
print(uuid.uuid1())
print(uuid.uuid3(uuid.NAMESPACE_DNS, 'zhangsan'))
print(uuid.uuid5(uuid.NAMESPACE_DNS, 'zhangsan'))
print(uuid.uuid4())
使用第三方模块
from flask import Flask
import sys
print(sys.path)
使用自定义模块
import my_module
from demo import *
print(my_module.a)
my_module.test()
print(my_module.add(1, 2))
print(m)
test()
from hello import *
print(x)
print(y)
包的使用
from chat import recv_msg
from chat.send_msg import x
import json
import flask
import chat
print(recv_msg.y)
print(x)
print(chat.recv_msg.y)
面向过程
def add_info():
pass
def del_info():
pass
def modify_info():
pass
def query_info():
pass
def show_all():
pass
def start():
while True:
print("""--------------------
名片管理系统 v1.0
1.添加名片
2.删除名片
3.修改名片
4.查询名片
5.显示所有名片
6.退出系统
--------------------""")
operator = input('请要进行的操作(数字)')
if operator == '1':
add_info()
elif operator == '2':
del_info()
elif operator == '3':
modify_info()
elif operator == '4':
query_info()
elif operator == '5':
show_all()
elif operator == '6':
pass
else:
print('输入有误,请重新输入......')
if __name__ == '__main__':
start()
面向对象的介绍
class Student(object):
def __init__(self, name, age, height):
self.name = name
self.age = age
self.height = height
def __int__(self):
pass
def run(self):
print('正在跑步')
def eat(self):
print('正在吃饭')
s1 = Student('小明', 18, 1.75)
s1.run()
s1.eat()
s2 = Student('小美', 17, 1.65)
s2.eat()
self语句的使用
class Student(object):
__slots__ = 'name'
def __init__(self, x, y):
self.name = x
self.age = y
def say_hello(self):
print('大家好,我是', self.name)
s1 = Student('张三', 18)
print('0x%X' % id(s1))
s2 = Student('jack', 21)
s2.say_hello()
s1.city = '上海'
print(s1.city)
s1.name = 'jiaxi'
print(s1.name)
魔法方法
import time
import datetime
x = datetime.datetime(2020, 2, 24, 16, 17, 45, 200)
print(x)
print(repr(x))
class Person(object):
def __init__(self, name, age):
print('__init__方法被调用了')
self.name = name
self.age = age
def __del__(self):
print('__del__方法被调用了')
def __repr__(self):
return 'hello'
def __str__(self):
return '姓名:{}, 年龄:{}'.format(self.name, self.age)
def __call__(self, *args, **kwargs):
print('args={}, kwargs={}'.format(args, kwargs))
test = kwargs['fn']
return test(args[0], args[1])
p = Person('zhangsan', 18)
print(p)
print(repr(p))
print(p.__repr__())
n = p(1, 2, fn=lambda x, y: x + y)
print(n)
运算符相关魔法方法
class Person(object):
def __init__(self, name, age):
self.name = name
self.age = age
def __eq__(self, other):
print('__eq__方法被调用了,other=', other)
return self.name == other.name and self.age == other.age
p1 = Person('zhangsan', 18)
p2 = Person('zhangsan', 18)
p3 = Person('zhangsan', 23)
print('0x%X' % id(p1))
print('0x%X' % id(p2))
print(p1 is p2)
print(p1 == p2)
print(p1 == p3)
nums1 = [1, 2, 3]
nums2 = [1, 2, 3]
print(nums1 is nums2)
print(nums1 == nums2)