本系列博文基于廖雪峰老师的官网Python教程,笔者在大学期间已经阅读过廖老师的Python教程,教程相当不错,官网链接: 廖雪峰官方网站.请需要系统学习Python的小伙伴到廖老师官网学习,笔者的编程环境是Anaconda+Pycharm,Python版本:Python3.
1.定制类
class Employee(object):
def __init__(self, name):
self.name = name
print("打印一个实例:", Employee("Willard."))
class Employee(object):
def __init__(self, name):
self.name = name
def __str__(self):
return "Employee object (name:%s)" % self.name
print("使用__str__打印一个实例:", Employee("Willard"))
class Fib(object):
def __init__(self):
self.a, self.b = 0, 1
def __iter__(self):
return self
def __next__(self):
self.a, self.b = self.b, self.a + self.b
if self.a > 100000:
raise StopIteration()
return self.a
print("Fib实例作用于for循环!")
for n in Fib():
print(n,end = " ")
class Fib(object):
def __getitem__(self, n):
a, b = 1, 1
for x in range(n):
a, b = b, a + b
return a
f = Fib()
print("Fib[0]的值为:", f[0])
print("Fib[100]的值为:", f[100])
class Fib(object):
def __getitem__(self, n):
if isinstance(n, int):
a, b = 1, 1
for x in range(n):
a, b = b, a + b
return a
if isinstance(n, slice):
start = n.start
stop = n.stop
if start is None:
start = 0
a, b = 1, 1
L = []
for x in range(stop):
if x >= start:
L.append(a)
a, b = b, a + b
return L
f = Fib()
print("f[0:5]的值为:", f[0:5])
print("f[100:150]的值为:", f[100:102])
class Employee(object):
def __init__(self, name):
self.name = name
employee1 = Employee("Willard")
print("employee1.name的值为:", employee1.name)
class Employee(object):
def __init__(self, name):
self.name = name
def __getattr__(self, attr):
if attr == "salary":
return 20000
employee2 = Employee("LinWenYu")
print("employee2.salary的值为:", employee2.salary)
class Employee(object):
def __init__(self, name, salary):
self.name = name
self.salary = salary
def __call__(self):
print("This Employee's name is %s.\nThe salary of %s is %d." % (self.name, self.name, self.salary))
employee = Employee("LinWenYu", 20000)
employee()
2.使用枚举类
JAN = 1
FEB = 2
MAR = 3
NOV = 11
DEC = 12
from enum import Enum
Month = Enum("Month", ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"))
for name, member in Month.__members__.items():
print(name, "=>", member, ",", member.value)
Jan => Month.Jan , 1
Feb => Month.Feb , 2
Mar => Month.Mar , 3
Apr => Month.Apr , 4
May => Month.May , 5
Jun => Month.Jun , 6
Jul => Month.Jul , 7
Aug => Month.Aug , 8
Sep => Month.Sep , 9
Oct => Month.Oct , 10
Nov => Month.Nov , 11
Dec => Month.Dec , 12
from enum import Enum, unique
@unique
class Weekday(Enum):
Sun = 0
Mon = 1
Tue = 2
Wed = 3
Thu = 4
Fri = 5
Sat = 6
day1 = Weekday.Mon
print("The value of Day1:", day1)
print("The value of Day1:", Weekday(1))
print("The value of Day2:", Weekday.Tue)
print("The value of Day2:", Weekday["Tue"])
print("The value of Weekday.Wed is:", Weekday.Wed.value)
print("------------------------------------------------")
for name, member in Weekday.__members__.items():
print(name, "=>", member)
print(name, ".value", "=>", member.value, "\n")
The value of Day1: Weekday.Mon
The value of Day1: Weekday.Mon
The value of Day2: Weekday.Tue
The value of Day2: Weekday.Tue
The value of Weekday.Wed is: 3
------------------------------------------------
Sun => Weekday.Sun
Sun .value => 0
Mon => Weekday.Mon
Mon .value => 1
Tue => Weekday.Tue
Tue .value => 2
Wed => Weekday.Wed
Wed .value => 3
Thu => Weekday.Thu
Thu .value => 4
Fri => Weekday.Fri
Fri .value => 5
Sat => Weekday.Sat
Sat .value => 6
3.使用元类
"""
class Welcome(object):
def welcome(self, welcomeString = "FUXI Technology."):
print("Welcome to %s" % welcomeString)
"""
from welcome import Welcome
w = Welcome()
w.welcome()
print("Welcome的类型为:", type(Welcome))
print("w的类型为:", type(w))
print("-------------------------------------")
def fn(self, welcomeString = "FUXI Technology."):
print("Welcome to %s" % welcomeString)
Welcome = type("Welcome", (object,), dict(welcome = fn))
w = Welcome()
w.welcome()
print("Welcome的类型:", type(Welcome))
print("w的类型:", type(w))
Welcome to FUXI Technology.
Welcome的类型为: <class 'type'>
w的类型为: <class 'welcome.Welcome'>
-------------------------------------
Welcome to FUXI Technology.
Welcome的类型: <class 'type'>
w的类型: <class '__main__.Welcome'>
class ListMetaclass(type):
def __new__(cls, name, bases, attrs):
attrs["add"] = lambda self, value: self.append(value)
return type.__new__(cls, name, bases, attrs)
class MyList(list, metaclass = ListMetaclass):
pass
L = MyList()
L.add(1)
print("L.add(1):", L)
|