Python:将类名作为参数传递给函数?

标签 python function

class TestSpeedRetrieval(webapp.RequestHandler):
  """
  Test retrieval times of various important records in the BigTable database 
  """
  def get(self):
      commandValidated = True 
      beginTime = time()
      itemList = Subscriber.all().fetch(1000) 

      for item in itemList: 
          pass 
      endTime = time()
      self.response.out.write("<br/>Subscribers count=" + str(len(itemList)) + 
           " Duration=" + duration(beginTime,endTime)) 

如何将上述内容转换为传递类名的函数? 在上面的示例中,Subscriber(在 Subscriber.all().fetch 语句中)是一个类名,这是您使用 Python 在 Google BigTable 中定义数据表的方式。

我想做这样的事情:

       TestRetrievalOfClass(Subscriber)  
or     TestRetrievalOfClass("Subscriber")  

谢谢, 尼尔·沃尔特斯

最佳答案

class TestSpeedRetrieval(webapp.RequestHandler):
  """
  Test retrieval times of various important records in the BigTable database 
  """
  def __init__(self, cls):
      self.cls = cls

  def get(self):
      commandValidated = True 
      beginTime = time()
      itemList = self.cls.all().fetch(1000) 

      for item in itemList: 
          pass 
      endTime = time()
      self.response.out.write("<br/>%s count=%d Duration=%s" % (self.cls.__name__, len(itemList), duration(beginTime,endTime))

TestRetrievalOfClass(Subscriber)  

关于Python:将类名作为参数传递给函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1436444/

相关文章:

jquery - 如何使用href调用jquery函数

c# - 使用数组进行独特行程选择的最佳性能算法?

python - 检查函数的返回值是否有 "waiting for"

c - 这个指针示例如何不产生错误?

python - Matplotlib/ basemap : Plot a globe in the center of a plot

python - 取第一个和中间条目、第二个和中间+1个条目的点积,直到middle-1和最后一个条目python/numpy

function - AHK : Stop one hotkey from triggering another hotkey

function - 如何将 Scala 函数作为函数传递给另一个函数?

python - 多级 argparse 子解析器

javascript - Raspberry Pi 的 PHP/HTML 通信