如何用python的for实现死循环
在python黑魔法指南上看到这个题目,想了一个实现记录在此
• 1 min read
import logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.WARNING)
console = logging.StreamHandler()
logger.addHandler(console)
formatter = logging.Formatter(fmt='%(asctime)s %(message)s',datefmt='%Y-%m-%d,%H:%M:%S.%f')
console.setFormatter(formatter)
# 遍历一个数组时,操作这个数组不断在后边插入,同时删除已遍历的元素
inc_list = [0]
for i in inc_list:
logging.warning('looping...')
del i
inc_list.append(0)
WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping... WARNING:root:looping...
--------------------------------------------------------------------------- KeyboardInterrupt Traceback (most recent call last) <ipython-input-1-21798d70db2e> in <module> 10 inc_list = [0] 11 for i in inc_list: ---> 12 logging.warning('looping...') 13 del i 14 inc_list.append(0) ~\Anaconda3\envs\tensorflow\lib\logging\__init__.py in warning(msg, *args, **kwargs) 1885 if len(root.handlers) == 0: 1886 basicConfig() -> 1887 root.warning(msg, *args, **kwargs) 1888 1889 def warn(msg, *args, **kwargs): ~\Anaconda3\envs\tensorflow\lib\logging\__init__.py in warning(self, msg, *args, **kwargs) 1318 """ 1319 if self.isEnabledFor(WARNING): -> 1320 self._log(WARNING, msg, args, **kwargs) 1321 1322 def warn(self, msg, *args, **kwargs): ~\Anaconda3\envs\tensorflow\lib\logging\__init__.py in _log(self, level, msg, args, exc_info, extra, stack_info) 1442 record = self.makeRecord(self.name, level, fn, lno, msg, args, 1443 exc_info, func, extra, sinfo) -> 1444 self.handle(record) 1445 1446 def handle(self, record): ~\Anaconda3\envs\tensorflow\lib\logging\__init__.py in handle(self, record) 1452 """ 1453 if (not self.disabled) and self.filter(record): -> 1454 self.callHandlers(record) 1455 1456 def addHandler(self, hdlr): ~\Anaconda3\envs\tensorflow\lib\logging\__init__.py in callHandlers(self, record) 1514 found = found + 1 1515 if record.levelno >= hdlr.level: -> 1516 hdlr.handle(record) 1517 if not c.propagate: 1518 c = None #break out ~\Anaconda3\envs\tensorflow\lib\logging\__init__.py in handle(self, record) 863 self.acquire() 864 try: --> 865 self.emit(record) 866 finally: 867 self.release() ~\Anaconda3\envs\tensorflow\lib\logging\__init__.py in emit(self, record) 994 msg = self.format(record) 995 stream = self.stream --> 996 stream.write(msg) 997 stream.write(self.terminator) 998 self.flush() ~\Anaconda3\envs\tensorflow\lib\site-packages\ipykernel\iostream.py in write(self, string) 408 self.flush() 409 else: --> 410 self._schedule_flush() 411 412 def writelines(self, sequence): ~\Anaconda3\envs\tensorflow\lib\site-packages\ipykernel\iostream.py in _schedule_flush(self) 332 def _schedule_in_thread(): 333 self._io_loop.call_later(self.flush_interval, self._flush) --> 334 self.pub_thread.schedule(_schedule_in_thread) 335 336 def flush(self): ~\Anaconda3\envs\tensorflow\lib\site-packages\ipykernel\iostream.py in schedule(self, f) 203 self._events.append(f) 204 # wake event thread (message content is ignored) --> 205 self._event_pipe.send(b'') 206 else: 207 f() ~\Anaconda3\envs\tensorflow\lib\site-packages\zmq\sugar\socket.py in send(self, data, flags, copy, track, routing_id, group) 398 copy_threshold=self.copy_threshold) 399 data.group = group --> 400 return super(Socket, self).send(data, flags=flags, copy=copy, track=track) 401 402 def send_multipart(self, msg_parts, flags=0, copy=True, track=False, **kwargs): zmq/backend/cython/socket.pyx in zmq.backend.cython.socket.Socket.send() zmq/backend/cython/socket.pyx in zmq.backend.cython.socket.Socket.send() zmq/backend/cython/socket.pyx in zmq.backend.cython.socket._send_copy() ~\Anaconda3\envs\tensorflow\lib\site-packages\zmq\backend\cython\checkrc.pxd in zmq.backend.cython.checkrc._check_rc() KeyboardInterrupt: