Python BIFS的可能的实现
头部
from sys import stdin,stdout
_no_arg=object()
abs()
def abs(x,/):
if hasattr(x,'__abs__'):
return x.__abs__()
else:
raise TypeError(f"bad operand type for abs(): '{x.__class__.__name__}'")
all()
def all(iterable,/):
for i in iterable:
if not i:
return False
return True
any()
def any(iterable,/):
for i in iterable:
if i:
return True
return False
callable()
def callable(cls,/):
return hasattr(cls,'__call__')
divmod()
def divmod(a,b,/):
if hasattr(a,'__divmod__'):
temp=a.__divmod__(b)
elif type(a) is type(b):
raise TypeError(f"unsupported operand type(s) for divmod(): '{a.__class__.__name__}' and '{b.__class__.__name__}'")
if temp is NotImplemented:
if hasattr(b,'__rdivmod__'):
temp=b.__rdivmod__(a)
else:
raise TypeError(f"unsupported operand type(s) for divmod(): '{a.__class__.__name__}' and '{b.__class__.__name__}'")
if temp is NotImplemented:
raise TypeError(f"unsupported operand type(s) for divmod(): '{a.__class__.__name__}' and '{b.__class__.__name__}'")
return temp
enumerate()
class enumerate:
def __init__(self,iterable,start=0):
self.__idx=start
self.__it=iter(iterable)
def __iter__(self):
return self
def __next__(self):
a=self.__it.__next__()
b=self.__idx
self.__idx+=1
return a,b
def __reduce__(self):
return self.__class__,(self.__it,self.__idx)
filter()
def filter(function, iterable,/):
if function is None:
for i in iterable:
if i:
yield i
else:
for i in iterable:
if function(i):
yield i
hash()
def hash(obj,/):
if obj.__hash__ is None:
raise TypeError(f"unhashable type: '{obj.__class__.__name__}'")
else:
temp=obj.__hash__()
if int in obj.__class__.__mro__:
return temp
else:
raise TypeError("__hash__ method should return an integer")
input()
def input(prompt=_no_arg,/):
if prompt is not _no_arg:
stdout.write(prompt.__str__())
return stdin.readline()[:-1]
isinstance()
def isinstance(obj,class_or_tuple,/):
try:
return issubclass(obj.__class__,class_or_tuple)
except TypeError:
raise TypeError("isinstance() arg 2 must be a type or tuple of types")
iter()
class iter:
def __new__(cls,a,b=_no_arg,/):
if b is _no_arg:
if hasattr(a,'__iter__'):
temp=a.__iter__()
else:
raise TypeError(f"'{a.__class__.__name__}' object is not iterable")
if hasattr(temp,'__next__'):
return temp
else:
raise TypeError(f"iter() returned non-iterator of type '{temp.__class__.__name__}'")
else:
return object.__new__(cls)
def __init__(self,callable,sentinel,/):
if hasattr(callable,'__call__'):
self.__a=callable
self.__b=sentinel
self.__c=False
else:
raise TypeError("iter(v, w): v must be callable")
def __iter__(self,/):
return self
def __next__(self):
if self.__c:
raise StopIteration
temp=self.__a()
if temp==self.__b:
self.__c=True
raise StopIteration
else:
return temp
def __reduce__(self):
if self.__c:
return self.__class__,((),)
else:
return self.__class__,(self.__a,self.__b)
len()
def len(obj,/):
if hasattr(obj,'__len__'):
return obj.__len__()
else:
raise TypeError(f"object of type '{obj.__class__.__name__}' has no len()")
map()
class map:
def __init__(self,func,/,*iterables):
if iterables:
self.__func=func
self.__iterators=tuple(iter(i) for i in iterables)
else:
raise TypeError("map() must have at least two arguments.")
def __reduce__(self):
return self.__class__,self.__iterators
def __iter__(self):
return self
def __next__(self):
return self.__func(*[next(i) for i in self.__iterators])
max()
def max(iterable,/,*vars,key=None,default=_no_arg):
if vars:
if default is _no_arg:
greatest=iterable
else:
raise TypeError("Cannot specify a default for max() with multiple positional arguments")
else:
vars=iter(iterable)
try:
greatest=next(vars)
except StopIteration:
if default is None:
raise ValueError("max() arg is an empty sequence")
else:
return default
if key is None:
for var in vars:
if var>greatest:
greatest=var
else:
flag=key(greatest)
for var in vars:
temp=key(var)
if temp>flag:
flag=temp
greatest=var
return greatest
min()
def min(iterable,/,*vars,key=None,default=_no_arg):
if vars:
if default is _no_arg:
least=iterable
else:
raise TypeError("Cannot specify a default for min() with multiple positional arguments")
else:
vars=iter(iterable)
try:
least=next(vars)
except StopIteration:
if default is None:
raise ValueError("min() arg is an empty sequence")
else:
return default
if key is None:
for var in vars:
if var<least:
least=var
else:
flag=key(least)
for var in vars:
temp=key(var)
if temp<flag:
flag=temp
least=var
return least
next()
def next(iterator,default=_no_arg,/):
if hasattr(iterator,'__next__') and callable(iterator.__next__):
if default is _no_arg:
return iterator.__next__()
else:
try:
return iterator.__next__()
except StopIteration:
return default
else:
raise TypeError(f"'{iterator.__class__.__name__}' object is not an iterator")
print()
def print(*values,sep=None,end=None,file=None,flush=False):
if sep is None:
sep=' '
elif not isinstance(sep,str):
raise TypeError(f'sep must be None or a string, not {sep.__class__.__name__}')
if end is None:
end='\n'
elif not isinstance(end,str):
raise TypeError(f'end must be None or a string, not {end.__class__.__name__}')
if file is None:
file=stdout
file.write(sep.join((str(i) for i in values))+end)
if flush:
file.flush()
reversed()
class reversed:
def __new__(cls,sequence,/):
if hasattr(sequence,'__reversed__'):
return sequence.__reversed__()
return object.__new__(cls)
def __init__(self,sequence,/):
if hasattr(sequence,'__getitem__'):
self.__idx=len(sequence)
else:
raise TypeError(f"'{sequence.__class__.__name__}' object is not reversible")
self.__seq=sequence
def __iter__(self):
return self
def __length_hint__(self):
return self.__idx+1
def __reduce__(self):
return self.__class__,(self.__seq,),self.__idx
def __next__(self):
if self.__idx:
self.__idx-=1
return self.__seq[self.__idx]
else:
raise StopIteration
def __setstate__(self,value):
if isinstance(value,int):
self.__index=min(value,self.__index)
else:
raise TypeError("an integer is required")
sum()
def sum(iterable,/,start=0):
for i in iterable:
start+=i
return start
zip()
class zip:
def __init__(self,*iterables):
self.__iterators=tuple(iter(i) for i in iterables)
def __iter__(self):
return self
def __next__(self):
return tuple([next(i) for i in self.__iterators])
def __reduce__(self):
return self.__class__,self.__iterators