0

I am writing a class where I would like to pass function as a class attribute and later use it, like that:

class Nevronska_mreza:
    def __init__(self, st_vhodni, st_skriti, st_izhod, prenosna_funkcija=pf.sigmoid):
        self.mreza = []
        self.st_vhodni = st_vhodni
        self.st_skriti = st_skriti
        self.st_izhodni = st_izhod
        self.prenosna_funckija = prenosna_funkcija
        self.mreza.append([{'utezi': [random() for i in range(st_vhodni + 1)]} for j in range(st_skriti)])
        self.mreza.append([{'utezi': [random() for i in range(st_skriti + 1)]} for j in range(st_izhod)])

    def razsirjanje_naprej(self, vhod):
        for sloj in self.mreza:
            nov_vhod = []
            for nevron in sloj:
                nevron['izhod'] = self.prenosna_funkcija(self.aktivacijska_funkcija(nevron['utezi'], vhod))
                nov_vhod.append(nevron['izhod'])
            vhod = nov_vhod
        return vhod

but it seems like this isn't the right way, I get the following error:

AttributeError: 'Nevronska_mreza' object has no attribute 'prenosna_funkcija'

Is it possible to do something like that?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
david1201
  • 29
  • 8
  • Does this answer your question? [Python - function as class attribute becomes a bound method](https://stackoverflow.com/questions/35321744/python-function-as-class-attribute-becomes-a-bound-method) – sahasrara62 Jan 04 '21 at 01:47

2 Answers2

2

Yes you can pass a function around as an argument however you have made a couple of mistakes.

Firstly you have used the word function, although not a reserved word it should be avoided as a name of an entity such as a variable.

Secordly you have used an optional parameter before mandatory parameters which will cause an error such as:

File "test.py", line 5
    def __init__(self, function=fun1, data1, data2):
            ^
SyntaxError: non-default argument follows default argument

Thirdly when calling the method you have not specified the scope, the function name is in the self scope of the object.

Taking all of these into account the following is working code

def fun1(x):
   return x+1

class A:
    def __init__(self, data1, data2, fn=fun1):
        self.fn = fn
        self.data1 = data1
        self.data2 = data2

    def some_method(self):
        y = self.fn(self.data1)
        print(y)

b = A(1, 2, fun1)
b.some_method()

After posting your full code I can see that you currently have self.prenosna_funckija instead of prenosna_funkcija in the following line:

self.prenosna_funckija = prenosna_funkcija

This would explain the attribute error as when you are calling self.prenosna_funkcija it genuinely does not exist.

Peter
  • 773
  • 1
  • 7
  • 23
  • I fixed both mistakes, but I am still getting Attribute error for fn. – david1201 Jan 04 '21 at 01:57
  • hard to diagnose without seeing the current state of the code and the exact error, pythons errors are usually quite good so point to the correct piece of code, the code above works perfectly for me – Peter Jan 04 '21 at 01:59
  • I didn't want to publish original code firstly because is not in English, but I did now anyway, it might be easier. – david1201 Jan 04 '21 at 02:04
  • posting the error message woould also help if possible – Peter Jan 04 '21 at 02:07
  • 2
    actually in this instance you have a typo `self.prenosna_funckija` instead of `self.prenosna_funkcija` – Peter Jan 04 '21 at 02:09
1

You're close:

def fun1(x):
return x+1

class A:
    def __init__(self, function=fun1, data1=None, data2=None):
        self.function = function
        self.data1 = data1
        self.data2 = data2

    def some_method(self):
        y = self.function(self.data1)
        return y

a = A(data1 = 41)
result = a.some_method()
print(result)

prints

42
couka
  • 1,361
  • 9
  • 16