随便写写
Posted wudeng
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了随便写写相关的知识,希望对你有一定的参考价值。
# _*_ coding: utf-8 _*_
__author__ = ‘pythonwu‘
__date__ = "2019/1/4 18:06"
#鸭子类型
class Duck(object):
def quack(self):
print(‘quack‘)
class Bird(object):
def quack(self):
print(‘bird imitate duck‘)
def in_the_same(duck):
duck.quack()
duck = Duck()
bird = Bird()
for x in [duck,bird]:
in_the_same(x)
import math
class Point(object):
def __init__(self,x,y):
self.x = x
self.y = y
def __sub__(self, other):
return Point(self.x- other.x ,self.y - other.y)
def __add__(self, other):
return Point(self.x+other.x,self.y+other.y)
@property
def xy(self):
return (self.x,self.y)
def __str__(self):
return str(self.xy)
class Shape(object):
def __init__(self,points_list):
for point in points_list:
assert isinstance(point,Point),"input must be Point type"
self.points = []
for point in points_list:
self.points.append(point.xy)
@property
def area(self):
return 0
def __bool__(self):
return self.area >0
def __lt__(self, other):
return self.area < other.area
def __gt__(self, other):
return self.area > other.area
class RectAngle(Shape):
def __init__(self,start_p,w,h,**kwargs):
self.w = w
self.h = h
self.start_p = start_p
super(RectAngle,self).__init__([start_p,start_p+Point(w,0),start_p+Point(w,h),start_p+Point(0,h)])
@property
def area(self):
return self.w*self.h
def __contains__(self, point):
return (point.x <= self.start_p.x +self.w and point.x >= self.start_p.x) and (point.y <= self.start_p.y +self.h and point.y> self.start_p.y)
start_p = Point(50,60)
a = RectAngle(start_p,100,80)
print(start_p in a )
print(a.points)
A = {‘name‘:‘xiaowang‘,‘age‘:‘15‘}
# print(A.name)
class A(object):
def __init__(self,name,age):
self.name = name
self.age = age
def __getattr__(self, attr):
if self.__dict__.get(‘attr‘,None) is None:
return self.__dict__.get(‘_‘ +attr ,None)
else:
return self.__dict__.get(attr,None)
def __setattr__(self, attr, value):
if attr in [‘name‘,‘age‘]:
if value <0 :
self.__dict__[‘_‘ +attr] = 0
else:
self.__dict__[‘_‘+ attr] = value
else:
self.__dict__[attr] = value
a = A(1,2)
print(a.name)
以上是关于随便写写的主要内容,如果未能解决你的问题,请参考以下文章