0

I have part of a function I would like to turn into another function. I want this new function to be able to edit the variables in in the parent function. Is this possible in python.

I know in other languages that a class can inherent their parents variables and function. I am wondering if there is something similar to this in python?

racksmey
  • 25
  • 6
  • just write a sub_function within the function and call it as though the variables were in the global environment – Onyambu May 22 '20 at 00:54
  • Please shar emore details. What do you mean by "able to edit the variables in the parent function"? – Nico Haase May 22 '20 at 07:09

2 Answers2

1

check here for scoping then here and here for closures. You are ideally looking for enclosing functions. The variables defined within the enclosing functions are available to the sub-functions as though they were globally defined. Most of these are widely dealt with in other languages.

def m(x,y):
     z = 2
     j = 4
     def n():
        return x+y+z+j
     return n()

m(3,1)
10
Onyambu
  • 67,392
  • 3
  • 24
  • 53
  • This works thanks. I still would like to know why it works. – racksmey May 22 '20 at 16:11
  • @racksmey Its relates to scoping. The variable z and j are available to the function n. Although localized within the function m, they, they are treated as "global" to function n since they are within the enclosing environment (the parent environment of n) – Onyambu May 22 '20 at 17:08
  • @NicoHaase What explanations would I have given in this case? I mean it feels like tackling the whole scoping, and closures – Onyambu May 22 '20 at 17:28
  • @Onyambu I have tried modifying the example you gave, but I get an error. If i add a line before the return inside n(), for example x =2 I get an error: UnboundLocalError: local variable 'x' referenced before assignment – racksmey May 22 '20 at 20:40
  • @racksmey I do not get that error. It works perfecly. Probably thats not the line that has an error. The error message `local variable 'x' referenced before assignment` means you are trying to use `x` yet it cannot obtain `x` that is what it means. It has nothing with assignment – Onyambu May 22 '20 at 20:45
0

is that what you are looking for !

class Parent:
    # two class variables x,y
    x = 100
    y = 100

    def __init__(self):
        pass

    def sum_from_parent(self):
        return self.x+self.y


class Child(Parent):
    def __init__(self):
        super().__init__()  # init super class to create x,y variables

    def sum_child(self):
        # edit base class variables
        x_in_child = Parent.x+20
        y_in_child = Parent.y+20
        return(x_in_child+y_in_child)

c = Child()
print("sum in parent = ", c.sum_from_parent())
print("sum in child = ", c.sum_child())

answer will be

sum in parent =  200
sum in child =  240
Mohamed MosȜd
  • 138
  • 2
  • 8
  • Yes, but with functions, not classes. I have a function I would like to create a helper method with, but I do not want to pass 10 variables that only exist inside the function. – racksmey May 22 '20 at 00:29
  • You can't do it with functions in python , the most clear and efficient way to get it is by using class inheritance , in python there's no function inheritance – Mohamed MosȜd May 22 '20 at 09:54
  • The answer I was looking for was posted as an answer. – racksmey May 22 '20 at 16:12