0

I'm trying to call a method within a class MyClass and have its value returned.

class MyClass:
    def __init__(self):
        print " Class Initialized"

    def gather_path(self):
        self.tld_object = Tld.objects.get(id=3,FKToClient=User.pk)
        return self.tld_object

How do I return the value of self.tld_object by importing my class in Python intrepreter.

I'm importing like:

from MyApp.MyClass import gather_path()

I know, this is quite basic - I'm relatively new to OOP in Python.

how do I then call this method to return the value of return self.tld_object within gather_path() method?

4
  • Why do you have a class at all? why not make gather_path a module level function? Commented May 22, 2014 at 19:11
  • There are other methods like gather_<whatever> in the class , I'm specifically trying to use the return value from gather_path() Commented May 22, 2014 at 19:12
  • Wouldn't from MyApp.MyClass import gather_path() be invalid syntax? Commented May 22, 2014 at 19:18
  • Just follow and finish one of the recommended Python OOP courses. Unless you keep on asking questions on syntactic non-sense. Commented May 22, 2014 at 19:18

1 Answer 1

4

It depends on what you're trying to do, but typically, I think the code importing your class would look like this:

from MyApp import MyClass

my_instance = MyClass()
value = my_instance.gather_path()

The value variable will now contain tld_object's value.

If you don't want to instantiate MyClass in order to call get_path(), you need to make get_path() either a class or static method.

From your example, it's not clear that you need to set self.tld_object, since you're just returning it from gather_path() anyway, unless other methods are relying on that state under the hood. If you are, though, it would be better practice to declare it in __init__, even if you set it to None. Alternatively, if all instances of MyClass are going to use the same value of tld_object, you could make it a class variable, and just declare it outside of any method, like so:

class MyClass:
    tld_object = Tld.objects.get(id=3,FKToClient=User.pk)

    def gather_path(self):
        return self.tld_object

Not sure how much of that is relevant to your needs, it's a bit hard to tell from the given example. If I were writing this (given what you've said so far), I'd do the following:

class MyClass:
    def __init__(self):
        self.tld_object = Tld.objects.get(id=3,FKToClient=User.pk)
        # Maybe raise an exception or something if self.tld_object doesn't get set right

    # Example of how to access tld_object from another method
    def print_tld_object(self):
        print self.tld_object

If you need to reach tld_object from outside the class, you would do the following in the other module:

from MyApp import MyClass

my_instance = MyClass()
tld = my_instance.tld_object
Sign up to request clarification or add additional context in comments.

9 Comments

How will this possibly return a value though? Wouldn't I need "return value" at the end ?
Well, you would access my_instance.tld_object after calling gather_path(), though depending on your needs, that might not be what you want.
What I'm trying to do is use the value of self.tld_object within another method named newmethod that will use the value of self.tld_object. what is best practice for that? I really appreciate the answer
So, in your example, when you call self.tld_object = Tld.objects.get(id=3,FKToClient=User.pk), that makes it available to all other methods on that instance of MyClass. So other methods can just reference self.tld_object, assuming gather_path() was called beforehand. You should strongly consider moving the self.tld_object = Tld.objects.get(id=3,FKToClient=User.pk) line to your init method, or at least calling self.tld_object = None from init.
Is it bad for performance to have multiple methods to just do a simple query - kinda how self.tld_object is doing? Is it better to put these statements just all in the init method?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.