「Pyhon入門」インスタンスの生成サンプル

サンプルコード

#! /usr/bin/env python3
# .-*- coding:utf-8 .-*-
 
import time
import threading
import random
 
 
class Member(object):
  
 def __init__(self):
  time.sleep(random.randint(1,3))
 
 @classmethod
 def instance(cls, *args, **kwargs):
  if not hasattr(Member, "_instance"):
   Member._instance = Member(*args, **kwargs)
  return Member._instance
 
 
def task(arg):
 obj = Member.instance()
 print(obj)
 
for i in range(5):
 t = threading.Thread(target=task, args=[i,])
 t.start()

Python

Posted by arkgame