0

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__.
Dharman
  • 30,962
  • 25
  • 85
  • 135
minattosama
  • 263
  • 1
  • 9
  • 1
    Does this answer your question? [How to access outer class from an inner class?](https://stackoverflow.com/questions/2024566/how-to-access-outer-class-from-an-inner-class) – NicoCaldo May 16 '22 at 09:14
  • You can access the class as `self.Constructor`, but nested classes are generally a bad idea. – jonrsharpe May 16 '22 at 09:15
  • [This](https://www.adamsmith.haus/python/answers/how-to-access-a-variable-from-an-outer-class-in-an-inner-nested-class-in-python) might help you. – narsan May 16 '22 at 09:17
  • An answear like the one below would be better if possible. I read all of the above and don't really fit on my case – minattosama May 16 '22 at 09:21
  • @jonrsharpe, what is the right way if I may ask – minattosama May 16 '22 at 09:38
  • The right way will depend on the context, which you haven't provided. Your current implementation of Constructor doesn't even work in and of itself. – jonrsharpe May 16 '22 at 09:43

1 Answers1

1

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)
TheRavenSpectre
  • 367
  • 2
  • 11
  • if I have other function in the Constructor part, shoud I use the same config ? use staticmethod and without self? – minattosama May 16 '22 at 09:22
  • It doesn't _need_ to be, if it's not you can just instantiate a `Constructor` and call the method on that. – jonrsharpe May 16 '22 at 09:23
  • When I add a second function to the constructor and call it inside createFromA, it doesn't find the function – minattosama May 16 '22 at 09:25
  • Could you show us the new code – TheRavenSpectre May 16 '22 at 09:26
  • I changed the code – minattosama May 16 '22 at 09:30
  • 1
    Since now the methods inside `Constructor` are instance methods, you can only call them through creating an instance of the class `Constructor` through first creating an instance `var=A.Constructor()` then applying the methods on to `var` as such: `var.createFromA(argA)` – TheRavenSpectre May 16 '22 at 09:34
  • 1
    And btw for the method `modifA`, self must be passed in as the first parameter as it is an instance method, not a static method. So it would be like that: `def modifA(self, Abis)` – TheRavenSpectre May 16 '22 at 09:35
  • okay I understand, thank you. Do you think that this is the right way to do the exercise ? I will have multiple classes in the end and I don't know if this is the right way – minattosama May 16 '22 at 09:37
  • Is there a reason for the implementation of the class `Constructor` inside the outer class `A` though – TheRavenSpectre May 16 '22 at 09:39
  • Not really. I just want to call multiple classes into a function and I thought that the best way should be outside. If you have any suggestions I am open to – minattosama May 16 '22 at 09:44
  • 1
    In that case, just stick to the outer class `A` and leave the methods inside `Constructor`, inside `A` instead – TheRavenSpectre May 16 '22 at 09:52