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?

CodeTalk
  • 3,571
  • 16
  • 57
  • 92

1 Answers1

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
Community
  • 1
  • 1
Nacho
  • 451
  • 2
  • 9
  • How will this possibly return a value though? Wouldn't I need "return value" at the end ? – CodeTalk May 22 '14 at 19:19
  • Well, you would access my_instance.tld_object after calling gather_path(), though depending on your needs, that might not be what you want. – Nacho May 22 '14 at 19:24
  • 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 – CodeTalk May 22 '14 at 19:25
  • 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. – Nacho May 22 '14 at 19:29
  • 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? – CodeTalk May 22 '14 at 19:34
  • Not necessarily. In this case, it's more of a code cleanliness thing. If most methods in in MyClass are going to rely on tld_object being set, then you should set it `__init__` if you can. It depends on what you're trying to do. You can leave it in gather_paths() if you want, but I would make sure that you still add a line to `__init__` that initially sets `self.tld_object = None` (and check for None every time you try to reference `self.tld_object`). I can't comment on performance, because I have no idea what you're actually trying to do. – Nacho May 22 '14 at 19:38
  • Let me trying to explain more. So, I have a method gather_path() which does a simple data pull . I'm trying to use the result of gather_path() which has an attribute: self.path_object , within a different method named parse_paths(). How can I use the value of self.path_object from gather_path() method in parse_paths() method to just bring the values into the method parse_paths() for use within that method? Does that make sense? – CodeTalk May 22 '14 at 19:43
  • So essentially, if self.tld_object is within the _init_ method, I can always access the self.tld_object value within the class just using: self.tld_object ? – CodeTalk May 22 '14 at 19:59
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/54234/discussion-between-nacho-and-codetalk). – Nacho May 22 '14 at 20:04