Python:将类名作为参数传递给函数? [英] Python: Passing a class name as a parameter to a function?

查看:250
本文介绍了Python:将类名作为参数传递给函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  class TestSpeedRetrieval(webapp.RequestHandler):

测试BigTable数据库中各种重要记录的检索时间

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

for 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)
或TestRetrievalOfClass(Subscriber )

谢谢,
Neal Walters

解决方案

  class TestSpeedRetrieval(webapp.RequestHandler):

测试各种重要记录的检索时间BigTable数据库

def __init __(self,cls):
self.cls = cls

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

用于itemList中的项目:
通过
endTime = time()
self.response.out.write(< br />%s count =%d持续时间=%s%(self.cls .__ name__,len (itemList),duration(beginTime,endTime))

TestRetrievalOfClass(Subscriber)


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)) 

How can I turn the above into a function where I pass the name of the class? In the above example, Subscriber (in the Subscriber.all().fetch statement) is a class name, which is how you define data tables in Google BigTable with Python.

I want to do something like this:

       TestRetrievalOfClass(Subscriber)  
or     TestRetrievalOfClass("Subscriber")  

Thanks, Neal Walters

解决方案

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:将类名作为参数传递给函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆