나는 python 클래스를 instansiate하는 함수를 사용하고 있습니다.Python : TypeError : 'str'개체를 호출 할 수 없습니다.
여성용 내가 데이터베이스에서 클래스 이름을 저장 한 내가 다른 파일에 각 클래스 구조를 정의한 클래스 구조
from DB.models import ApiKey,ServiceProvider
class SMSMrg(object):
_instance = None
class Singleton:
def __init__(self):
self.username = None
self.password = None
self.allsp = []
self.classnames = {}
def __new__(cls, *args, **kwargs):
if not cls._instance:
cls._instance = super(SMSMrg, cls).__new__(
cls, *args, **kwargs)
return cls._instance
def loadsettings(self):
get_all_sp = ServiceProvider.objects.filter(status = False)
for (options,obj) in enumerate(get_all_sp):
cla = str(obj.class_Name)
self.classnames[cla] = cla
print self.classnames
for (options,obj) in enumerate(get_all_sp):
cla = str(obj.class_Name)
class_object = self.classnames[cla](obj.userName,obj.password,obj.sendingurl)
# self.allsp = get_all_sp
def send(self):
print "+++++++++++++++++++== Global send "
if __name__ == "__main__":
b = SMSMrg()
b.loadsettings()
입니다.
마찬가지로 cla
에는 클래스 이름이 포함됩니다.
하지만 위의 함수를 호출 할 때 형식 오류가 나타납니다.
Traceback (most recent call last):
File "allsms.py", line 30, in <module>
b.loadsettings()
File "allsms.py", line 21, in loadsettings
class_object = cla(obj.userName,obj.password,obj.sendingurl)
TypeError: 'str' object is not callable
이름은 내 DB에 존재하는 모든 클래스를 instansiate 수있는 방법을 가르쳐주세요.
obj''무엇입니까? 클래스의 인스턴스입니까? – Bakuriu
예 모델 클래스의 인스턴스 – masterofdestiny
클래스 내부에 클래스로 '싱글 톤'을 쓰는 이유는 무엇입니까? 또한'__new__'가 잘못 들여 쓰여 있거나 정말로'Singleton'이 아닌'SMSMrg'의 일부입니까? – Bakuriu