Using a function within a class in python (to use self or not)
Asked Answered
L

3

6
class Neuralnetwork(object):

     def __init__(self, data):    
         self.data = data

     def scan(self):
         print(self.data)

     def sigmoid(self, z):
         g = 1 / (1 + math.exp(-z))
         return (g)

     a1 = sigmoid(7)
     print a1

I'm not sure why it won't print the a1 variable with the sigmoid function. It keeps kicking off an error saying that it requires 2 inputs instead of 1. But I thought that by calling the function within the class, I didn'tneed to supply self to it again?

Edit: I have the last two statements in there because I'm still testing things out to make sure that everything is doing what it's supposed to within the class.

Laborious answered 14/4, 2017 at 14:44 Comment(8)
Because sigmoid is a function within the class definition. Why did you indent a1 = .. and print a1 to be part of the class? Why not put def sigmoid outside of the class definition if it is not meant to be a method?Easing
are you sure about the indentation of your two last line. Seems it should be out of the class.Bonspiel
If you were calling it from inside a class method, you would need to call it as self.sigmoid(7). If you wanted to call it from outside, then you would need to create an instance of Neuralnetwork to call obj.sigmoid(7) on.Actinium
You are right, you don't need self if you call from the class. But then, this call should occur from another method. You cannot have floating calls within a class as here.Hurling
I tried caling it as self.sigmoid(7), and that didn't work either.Laborious
if you call it INSIDE your class you have to put it in a methodBonspiel
Oh, I see. I can't just have a call to a method defined in my class as a part of my class?Laborious
see the two answers below, they should help you outBonspiel
E
1

sigmoid is a method of the Neuralnetwork class, so you need to create an instance of the Neuralnetwork class first, before you can utilize the sigmoid function, if you're calling it after the class definition:

class Neuralnetwork(object):
     def __init__(self, data):    
         self.data = data

     def scan(self):
         print(self.data)

     def sigmoid(self, z):
         g = 1 / (1 + math.exp(-z))
         return (g)

# replace data and z with appropriate values
nn = Neuralnetwork(data)
a1 = nn.sigmoid(z)
print a1

If you need to use it within the class, put the block within a method:

class Neuralnetwork(object):
     def __init__(self, data):    
         self.data = data

     def scan(self):
         print(self.data)

     def sigmoid(self, z):
         g = 1 / (1 + math.exp(-z))
         return (g)

     def print_sigmoid(self, z):
         a1 = self.sigmoid(z)
         print a1

# replace data and z with appropriate values
nn = Neuralnetwork(data)
nn.print_sigmoid(z)

I also recommend changing the class name to NeuralNetwork, as per the PEP 8 Style Guide: https://www.python.org/dev/peps/pep-0008/#class-names

Eyeleen answered 14/4, 2017 at 14:47 Comment(2)
Thank you! This makes a lot of sense.Laborious
You're welcome! @Laborious Please accept/vote the answers if they solve your problem.Eyeleen
D
4

I notice that your sigmoid method does not use self at all, that is, it does not depend on the instance. You may put it outside the class as a normal function. But if it is closely related to the class, you may prefer to enclose it as a static method, removing completely the self from the sigmoid def:

#/usr/bin/env python3

import math

class NeuralNetwork(object):

    def __init__(self, data):    
        self.data = data

    def scan(self):
        print(self.data)

    @staticmethod
    def sigmoid(z):
        g = 1 / (1 + math.exp(-z))
        return (g)

a1 = NeuralNetwork('abc')
print(a1.sigmoid(7))
Dinosaurian answered 14/4, 2017 at 15:30 Comment(0)
E
1

sigmoid is a method of the Neuralnetwork class, so you need to create an instance of the Neuralnetwork class first, before you can utilize the sigmoid function, if you're calling it after the class definition:

class Neuralnetwork(object):
     def __init__(self, data):    
         self.data = data

     def scan(self):
         print(self.data)

     def sigmoid(self, z):
         g = 1 / (1 + math.exp(-z))
         return (g)

# replace data and z with appropriate values
nn = Neuralnetwork(data)
a1 = nn.sigmoid(z)
print a1

If you need to use it within the class, put the block within a method:

class Neuralnetwork(object):
     def __init__(self, data):    
         self.data = data

     def scan(self):
         print(self.data)

     def sigmoid(self, z):
         g = 1 / (1 + math.exp(-z))
         return (g)

     def print_sigmoid(self, z):
         a1 = self.sigmoid(z)
         print a1

# replace data and z with appropriate values
nn = Neuralnetwork(data)
nn.print_sigmoid(z)

I also recommend changing the class name to NeuralNetwork, as per the PEP 8 Style Guide: https://www.python.org/dev/peps/pep-0008/#class-names

Eyeleen answered 14/4, 2017 at 14:47 Comment(2)
Thank you! This makes a lot of sense.Laborious
You're welcome! @Laborious Please accept/vote the answers if they solve your problem.Eyeleen
B
0

with the two last line outside your class (without indentation) you can modify them with:

a1 = Neuralnetwork(data).sigmoid(7)
print(a1)

but you have to give data to your class

Bonspiel answered 14/4, 2017 at 14:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.