Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9235229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:58:41+00:00 2026-06-18T06:58:41+00:00

I am using a function to instansiate the python classes . Hers is the

  • 0

I am using a function to instansiate the python classes .

Hers is the class structure

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

I have stored the classnames in database and I have defined each class structures on different files .

Like cla will contain a class name .

But when i am calling above function i am getting the type error .

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

Please tell me how can instansiate all the classes which names are present in my db .

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T06:58:42+00:00Added an answer on June 18, 2026 at 6:58 am

    As you said cla contains the name of the class, which means that you can’t use it as a callable.

    You can build a dict and take the class object from there:

    from somemodule import SomeClass
    
    class TheClass(object):
        def __init__(self, username, password, url):
            #do stuff
    
    class AnOtherClass(object):
        def __init__(self, username, password, url):
            # do stuff
    
    CLASS_NAMES_TO_CLASSES = {
        # Note: TheClass is *not* a string, is the class!!!
        'FirstName': TheClass,
        'SecondName': AnOtherClass,
        'SomeClass': SomeClass,
        }
    
    class SMSMrg(object):
        #do stuff
        def loadsettings(self):
           get_all_sp = ServiceProvider.objects.filter(status = True)
           for obj in get_all_sp:
               SERVIVEPROVIDER = obj.class_Name
               cla = str(SERVIVEPROVIDER)
               class_object = CLASS_NAMES_TO_CLASSES[cla](obj.userName,obj.password,obj.sendingurl)
    

    This method requires you to be able to build such a dict, so either you know ahead which classes could end up in the db or you can’t use this method.

    Note that CLASS_NAMES_TO_CLASSES is not a dictionary that maps strings to strings. It maps strings to class objects. If you import the class SomeClass from a module then you have to put it inside the dictionary.

    An other method could be to use eval to evaluate the class name, but you should avoid this if the db contains data from users(which is not safe).

    An other option that might turn out useful is to avoid saving the class names and instead use pickle to save the instances directly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a simple C class I have implemented, using function pointers in a
I'm using a Class Library exporting a function via COM. When calling this function
I want to detect existence of a specific member function for a class, using
I've wrapped a C++ class using Py++ and everything is working great in Python.
I have a class A which instantiates an object from class B internally. That
I want to make some C++ program and I'm using function popen here to
I'm using two functions one is from this site http://ryanfait.com/resources/custom-checkboxes-and-radio-buttons/ and its using function
I have a question regarding use of FFT. Using function getBand(int i) with Minim
using getdate function assign today to the today.but why today year assign to year.its
When using the function imagepng() in PHP, how can I make sure the images

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.