Python 3 연산 자 재 부팅 방법 예제
실제로'연산 자 리 셋'은 클래스 방법 에서 내 장 된 작업 을 차단 하 는 것 을 의미 할 뿐 입 니 다..............................................................................다음은 과부하 의 관건 적 인 개념 에 대한 복습 이다.
다음 코드 는 Python 3.6.1 을 예 로 들 면
연산 자 재 업로드 방법:클래스(class)는 특수 이름 을 사용 하 는 방법(len(self)을 통 해 특수 문법(len()호출 을 실현 합 니 다.
#coding=utf-8
# specialfuns.py
# (class) (__len__(self)) (len())
#
class demo1:
# ,
def __init__(self):
print(" ")
# ,
def __del__(self):
print(" ")
# new
class demo2(object):
# __init__ , __new__ , (http://blog.csdn.net/rozol/article/details/69317339)
def __new__(cls):
print("new")
return object.__new__(cls)
#
class demo3:
def __init__(self, num):
self.data = num
# +
def __add__(self, other):
return self.data + other.data
# -
def __sub__(self, other):
return self.data - other.data
# *
def __mul__(self, other):
return self.data * other.data
# /
def __truediv__(self, other):
return self.data / other.data
# //
def __floordiv__(self, other):
return self.data // other.data
# %
def __mod__(self, other):
return self.data % other.data
# divmod()
def __divmod__(self, other):
# (10/5), (10%5)
return self.data / other.data, self.data % other.data
# **
def __pow__(self, other):
return self.data ** other.data
# <<
def __lshift__(self, other):
return self.data << other.data
# >>
def __rshift__(self, other):
return self.data >> other.data
# &
def __and__(self, other):
return self.data & other.data
# ^
def __xor__(self, other):
return self.data ^ other.data
# |
def __or__(self, other):
return self.data | other.data
class none:
def __init__(self, num):
self.data = num
# (a+b, a , b )( : , +r)
class demo4:
def __init__(self, num):
self.data = num
# +
def __radd__(self, other):
return other.data + self.data
# -
def __rsub__(self, other):
return other.data - self.data
# *
def __rmul__(self, other):
return other.data * self.data
# /
def __rtruediv__(self, other):
return other.data / self.data
# //
def __rfloordiv__(self, other):
return other.data // self.data
# %
def __rmod__(self, other):
return other.data % self.data
# divmod()
def __rdivmod__(self, other):
return other.data / self.data, other.data % self.data
# **
def __rpow__(self, other):
return other.data ** self.data
# <<
def __rlshift__(self, other):
return other.data << self.data
# >>
def __rrshift__(self, other):
return other.data >> self.data
# &
def __rand__(self, other):
return other.data & self.data
# ^
def __rxor__(self, other):
return other.data ^ self.data
# |
def __ror__(self, other):
return other.data | self.data
# ,( : , +i)
class demo5():
def __init__(self, num):
self.data = num
# +=
def __iadd__(self, other):
return self.data + other
# -=
def __isub__(self, other):
return self.data - other
# *=
def __imul__(self, other):
return self.data * other
# /=
def __itruediv__(self, other):
return self.data / other
# //=
def __ifloordiv__(self, other):
return self.data // other
# %=
def __imod__(self, other):
return self.data % other
# **=
def __ipow__(self, other):
return self.data ** other
# <<=
def __ilshift__(self, other):
return self.data << other
# >>=
def __irshift__(self, other):
return self.data >> other
# &=
def __iand__(self, other):
return self.data & other
# ^=
def __ixor__(self, other):
return self.data ^ other
# |=
def __ior__(self, other):
return self.data | other
#
class demo6:
def __init__(self, num):
self.data = num
# <
def __lt__(self, other):
return self.data < other.data
# <=
def __le__(self, other):
return self.data <= other.data
# ==
def __eq__(self, other):
return self.data == other.data
# !=
def __ne__(self, other):
return self.data != other.data
# >
def __gt__(self, other):
return self.data > other.data
# >=
def __ge__(self, other):
return self.data >= other.data
#
class demo7:
def __init__(self, num):
self.data = num
# +
def __pos__(self):
return +abs(self.data)
# -
def __neg__(self):
return -abs(self.data)
# abs()
def __abs__(self):
return abs(self.data)
# ~
def __invert__(self):
return ~self.data
# complex()
def __complex__(self):
return 1+2j
# int()
def __int__(self):
return 123
# float()
def __float__(self):
return 1.23
# round()
def __round__(self):
return 1.123
#
class demo8:
# print()
def __str__(self):
return "This is the demo."
# repr()
def __repr__(self):
return "This is a demo."
# bytes()
def __bytes__(self):
return b"This is one demo."
# format()
def __format__(self, format_spec):
return self.__str__()
#
class demo9:
# ( )
def __getattr__(self):
print (" ")
# getattr() hasattr()
def __getattribute__(self, attr):
print (" %s"%attr)
return attr
# setattr()
def __setattr__(self, attr, value):
print (" %s %s"%(attr, value))
# delattr()
def __delattr__(self, attr):
print (" %s "%attr)
# ===================================================================
# ( (test1) (runtest) , )( : , )
class test1:
def __init__(self, value = 1):
self.value = value * 2
def __set__(self, instance, value):
print("set %s %s %s"%(self, instance, value))
self.value = value * 2
def __get__(self, instance, owner):
print("get %s %s %s"%(self, instance, owner))
return self.value
def __delete__(self, instance):
print("delete %s %s"%(self, instance))
del self.value
class test2:
def __init__(self, value = 1):
self.value = value + 0.3
def __set__(self, instance, value):
print("set %s %s %s"%(self, instance, value))
instance.t1 = value + 0.3
def __get__(self, instance, owner):
print("get %s %s %s"%(self, instance, owner))
return instance.t1
def __delete__(self, instance):
print("delete %s %s"%(self, instance))
del self.value
class runtest:
t1 = test1()
t2 = test2()
# ---
# property
class property_my:
def __init__(self, fget=None, fset=None, fdel=None):
self.fget = fget
self.fset = fset
self.fdel = fdel
# (self , instance (demo9), owner (demo9))
def __get__(self, instance, owner):
print("get %s %s %s"%(self, instance, owner))
return self.fget(instance)
#
def __set__(self, instance, value):
print("set %s %s %s"%(self, instance, value))
self.fset(instance, value)
#
def __delete__(self, instance):
print("delete %s %s"%(self, instance))
self.fdel(instance)
class demo10:
def __init__(self):
self.num = None
def setvalue(self, value):
self.num = value
def getvalue(self):
return self.num
def delete(self):
del self.num
x = property_my(getvalue, setvalue, delete)
# ===================================================================
#
class lis:
def __init__(self, *args):
self.lists = args
self.size = len(args)
self.startindex = 0
self.endindex = self.size
# len()
def __len__(self):
return self.size;
# lis[1]
def __getitem__(self, key = 0):
return self.lists[key]
# lis[1] = value
def __setitem__(self, key, value):
pass
# del lis[1]
def __delitem__(self, key):
pass
#
def __iter__(self):
return self
# rversed()
def __reversed__(self):
while self.endindex > 0:
self.endindex -= 1
yield self[self.endindex]
# next()
def __next__(self):
if self.startindex >= self.size:
raise StopIteration #
elem = self.lists[self.startindex]
self.startindex += 1
return elem
# in / not in
def __contains__(self, item):
for i in self.lists:
if i == item:
return True
return False
# yield ( , )
def yielddemo():
num = 0
while 1: # 1 == True; 0 == False
if num >= 10:
raise StopIteration
num += 1
yield num
#
def yielddemo_1():
while 1:
num = yield
print(num)
# with
class withdemo:
def __init__(self, value):
self.value = value
# as
def __enter__(self):
return self.value
# ,
def __exit__(self, exc_type, exc_value, traceback):
del self.value
if __name__ == "__main__":
#
d1 = demo1()
del d1
# new
d2 = demo2()
#
d3 = demo3(3)
d3_1 = demo3(5)
print(d3 + d3_1)
print(d3 - d3_1)
print(d3 * d3_1)
print(d3 / d3_1)
print(d3 // d3_1)
print(d3 % d3_1)
print(divmod(d3, d3_1))
print(d3 ** d3_1)
print(d3 << d3_1)
print(d3 >> d3_1)
print(d3 & d3_1)
print(d3 ^ d3_1)
print(d3 | d3_1)
#
d4 = none(3)
d4_1 = demo4(5)
print(d4 + d4_1)
print(d4 - d4_1)
print(d4 * d4_1)
print(d4 / d4_1)
print(d4 // d4_1)
print(d4 % d4_1)
print(divmod(d4, d4_1))
print(d4 ** d4_1)
print(d4 << d4_1)
print(d4 >> d4_1)
print(d4 & d4_1)
print(d4 ^ d4_1)
print(d4 | d4_1)
# ( )
d5 = demo5(3)
d5 <<= 5
d5 >>= 5
d5 &= 5
d5 ^= 5
d5 |= 5
d5 += 5
d5 -= 5
d5 *= 5
d5 /= 5
d5 //= 5
d5 %= 5
d5 **= 5
print(d5)
#
d6 = demo6(3)
d6_1 = demo6(5)
print(d6 < d6_1)
print(d6 <= d6_1)
print(d6 == d6_1)
print(d6 != d6_1)
print(d6 > d6_1)
print(d6 >= d6_1)
# ( )
d7 = demo7(-5)
num = +d7
num = -d7
num = abs(d7)
num = ~d7
print(num)
print(complex(d7))
print(int(d7))
print(float(d7))
print(round(d7))
#
d8 = demo8()
print(d8)
print(repr(d8))
print(bytes(d8))
print(format(d8, ""))
#
d9 = demo9()
setattr(d9, "a", 1) # => a 1
print(getattr(d9, "a")) # => a / a
print(hasattr(d9, "a")) # => True / a
delattr(d9, "a") # a
# ---
d9.x = 100 # => x 100
print(d9.x) # => x / x
del d9.x # => x
#
r = runtest()
r.t1 = 100 # => <__main__.test1> <__main__.runtest> 100
print(r.t1) # => 200 / <__main__.test1> <__main__.runtest> <class '__main__.runtest'>
del r.t1 # => <__main__.test1> <__main__.runtest>
r.t2 = 200 # => <__main__.test2> <__main__.runtest> 200 / <__main__.test1> <__main__.runtest> 200.3
print(r.t2) # => 400.6 / <__main__.test2> <__main__.runtest> <class '__main__.runtest'> / <__main__.test1> <__main__.runtest> <class '__main__.runtest'>
del r.t2 # <__main__.test2> <__main__.runtest>
# ---
# property
d10 = demo10()
d10.x = 100; # => <__main__.property_my> <__main__.demo10> 100
print(d10.x) # => 100 / <__main__.property_my> <__main__.demo10> <class '__main__.demo10'>
del d10.x # => <__main__.property_my> <__main__.demo10>
d10.num = 200;
print(d10.num) # => 200
del d10.num
# ( Iterator)
lis = lis(1,2,3,4,5,6)
print(len(lis))
print(lis[1])
print(next(lis))
print(next(lis))
print(next(lis))
for i in lis:
print (i)
for i in reversed(lis):
print (i)
print(3 in lis)
print(7 in lis)
print(3 not in lis)
print(7 not in lis)
# yield ( Iterable)
for i in yielddemo():
print (i)
# ---
iters = iter(yielddemo())
print(next(iters))
print(next(iters))
# --- ---
iters = yielddemo_1()
next(iters)
iters.send(6) #
iters.send(10)
# with
with withdemo("Less is more!") as s:
print(s)
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Python의 None과 NULL의 차이점 상세 정보그래서 대상 = 속성 + 방법 (사실 방법도 하나의 속성, 데이터 속성과 구별되는 호출 가능한 속성 같은 속성과 방법을 가진 대상을 클래스, 즉 Classl로 분류할 수 있다.클래스는 하나의 청사진과 같아서 하나의 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.