1

Hi everyone i wanna use a calculated value from a method of the class itself for the rest of the class methods but it must calculate once for all and i need to invoke method inside the class itself i write an example:

class something():
    def __init__():
        pass

    def __sum(self, variable_1, variable_2):
        self.summation = sum(variable_1, variable_2)

    # I need to calculate summation here once for all:
    # how does the syntax look likes, which one of these are correct:

    something.__sum(1, 2)
    self.__sum(1, 2)

    # If none of these are correct so what the correct form is?
    # For example print calculated value here in this method:

    def do_something_with_summation(self):
        print(self.summation)
Hamed Fathi
  • 65
  • 3
  • 11
  • The correct syntax should be `self.__sum(1, 2)`. If you receive the values of `variable1` and `variable2` at init you could also create a `self.summation` variable in the `__init__` method and use it in the other class methods. – toti08 Aug 15 '18 at 11:14
  • Inside your `__sum` method, you assign something to `self.summation` - but there is no `self` in that method. What do you expect this method to do? Is `summation` supposed to be a class variable? – Aran-Fey Aug 15 '18 at 11:15
  • Using double leading underscores for method names may cause some problems in the future attribute access due to Python name mangling, unless this is what you intended. Most of the time, single leading underscores are used for 'internal' methods/attributes – N Chauhan Aug 15 '18 at 11:28
  • @Aran-Fey i forgot to put self inside the methods i edit it now and fix it – Hamed Fathi Aug 15 '18 at 12:44
  • @N Chauhan yes i know this – Hamed Fathi Aug 15 '18 at 12:45
  • Inside the class definition, `__sum` is not yet a method of any type; it's just a function which expects 3 arguments. You could write `__sum(..., 1, 2)`, but it's not clear what you should replace `...` with, or what the intent is. (You're defining an instance method but trying to call it on the as-yet-undefined class object.) – chepner Aug 15 '18 at 13:13
  • i understand thanks for explanation so what should i do? how can i immediate calling a function inside the class or it is impossible? – Hamed Fathi Aug 15 '18 at 15:36

2 Answers2

0

Something like this seems to be what you're looking for:

class Something:
    def __init__(self):
        self.__sum(1, 2)

    def __sum(self, variable_1, variable_2):
        self.summation = sum(variable_1, variable_2)

Not saying this is the ideal approach or anything, but you haven't really given us much to go off of.

In general, make sure self is the first argument in all class methods, and you can call that class method at any time using either self.method_name() if you are using it from within another class method or instance.method_name() if you're using it externally (where instance = Something()).

Wiggy A.
  • 496
  • 3
  • 16
  • variable_1 also calculated after user use one of the methods so i need invoke __sum function after user use some of methods and enter a value for calculation – Hamed Fathi Aug 15 '18 at 12:53
  • Take a look at the edit; might help. And honestly, I'm having trouble understanding what you're looking for exactly. – Wiggy A. Aug 15 '18 at 12:59
  • please check the comments blew the question to understand my purpose – Hamed Fathi Aug 15 '18 at 15:40
  • I'm sorry, but I'm guessing it might be because of a language barrier, but I don't think anyone is clear about what you're asking for yet. Have a look at this; perhaps you want to call a class method from inside the class, but not from within another class method? https://stackoverflow.com/questions/13900515/how-can-i-access-a-classmethod-from-inside-a-class-in-python – Wiggy A. Aug 16 '18 at 05:28
0

Assuming that you would receive variable1 and variable2 when you instantiate the class one solution could be:

class something():
    def __init__(self, variable1, variable2):
        self.summation = variable1 + variable2

    def do_something_with_summation(self):
        print(self.summation)

If instead you're creating variable1 and variable2 inside other methods, then you could make them class variables:

class Something():
    def __init__(self):
        #Put some initialization code here

    def some_other_method(self):
        self.variable1 = something
        self.variable2 = something

    def sum(self):
        try:
            self.summation = self.variable1 + self.variable2
        except:
            #Catch your exception here, for example in case some_other_method was not called yet

    def do_something_with_summation(self):
        print(self.summation)
toti08
  • 2,448
  • 5
  • 24
  • 36