Home > Software design >  Edited _ Return an object from a class
Edited _ Return an object from a class

Time:05-19

I am re-writing the question since now I have a clear understanding.

Is it possible to return an object just by calling a class? Take for exemple, I am trying to connect to a client server and just by calling the class, I get the connection.

And if I want to start a new project on that server, I would only need to init the project and the connection is called automatically.

class LabellingProject():
    def __init__(self, 
                 web_app=False,
                 port=8888,
                 data_dir="/xxxx/xxx/",
                 local_files_root="/xxxx/datasets",
                 disable_signup_without_link=True,
                 username= "xxxx@xxxx"):
        self.web_app = web_app
        self.port = port
        self.data_dir = data_dir
        self.local_files_root = local_files_root
        self.disable_signup_without_link = disable_signup_without_link
        self.username = username

    def connection(cls):
        """
        """
        config = Config(web_app=cls.web_app,
                        port = cls.port,
                        data_dir = cls.data_dir,
                        local_files_root=cls.local_files_root,
                        disable_signup_without_link=cls.disable_signup_without_link,
                        username=cls.username)
        server = LabelStudioServer()
        server.start(config)
        client = Client(url="http://localhost:" str(config.port), 
                        api_key=config.token)

        return client
    
    def create_new(self,client):
        
        """
        """
        project = client.start_project()

        return project

I want to call the function like this.

labelling = LabellingProject() => it will return the connection

project = labelling.create_new() => it will return the project and the client it should take it from the __init__.

Is it more understanding?

CodePudding user response:

createFromA needs to be a staticmethod as it does not operate on instances of the class Constructor

class A:
    def __init__(self):
        pass
    def create_project(self,argA):
        
        newA = self.Constructor.createFromA(argA)
        
        return newA      
        
    class Constructor:
        def __init__(self):
            pass
        @staticmethod
        def createFromA(argA):
            A = 2*argA
            return A
        
        
cons = A()
cons.create_project(2)
  • Related