-2

I’m studying python through the paid online course and i got an error by typing following codes while studying module and pakages.

class Fibonacci:
    def __init__(self, title="fibonacci"):
        self.title = title

    def fib(n):
        a, b = 0, 1
        while a < n:
            print(a, end=' ')
            a, b = b, a + b
        print()

    def fib2(n):
        result = []
        a, b = 0, 1
        while a < n:
            result.append(a)
            a, b = b, a + b
        return result

and the def shows an error like "def Method should have "self" as first argument". do you know why am i having an error? i think my code should be okay, and when i try to run it though my friends laptop(window) it works well btw, I’m using mac os. sorry I’m just new to python .. :) click to see the error here

----- edited ----------------- thanks for the comments! and i have edited like the pictureedited code and it has no error! :)

but when i try to call the function, has an error like TypeError: fib() missing 1 required positional argument: 'n'

from pkg.fibonacci import Fibonacci

Fibonacci.fib(100)

see the error message error message2

Doyeon
  • 7
  • 7

3 Answers3

3

This is because all the functions within a class must have an argument named self if you want to bind the function to the class.

self represents the instance of the class. By using the self keyword we can access the attributes and methods of the class in python. It binds the attributes with the given arguments

Try This

class Fibonacci:
  def __init__(self, title="fibonacci"):
    self.title = title

  def fib(self,n):
    a, b = 0, 1
    while a < n:
        print(a, end=' ')
        a, b = b, a + b
    print()

  def fib2(self,n):
    result = []
    a, b = 0, 1
    while a < n:
        result.append(a)
        a, b = b, a + b
    return result

Refer Self in Python Class

Edit:

Answering your other question

An object should be used while calling the class functions. So you have to define an object before you call the function.

Like this

from pkg.fibonacci import Fibonacci
f = Fibonacci()
f.fib(100)
theWellHopeErr
  • 1,856
  • 7
  • 22
  • but i tried to include self and i don’t know how to edit my calling function. now i have -> Fibonacci.fib(100) and i don’t know what to put before 100. – Doyeon Dec 03 '20 at 04:57
  • i got an error like : TypeError: fib() missing 1 required positional argument: 'n' – Doyeon Dec 03 '20 at 05:04
  • @Doyeon I've edited my answer with a solution for this. – theWellHopeErr Dec 03 '20 at 05:14
  • 1
    You don't have to call it 'self'. You can call it whatever you want like 'Goku' and the code still runs. However, by PEP 8 convention, people call it 'self'. Some python editor tools (but not Python Interpreter) assume you call it 'self' and won't work if you call it otherwise. – Kamran Bigdely Jan 05 '22 at 16:03
1

Not sure if the fib / fib2 is the class method.

if they are, you may add self in the object parameter, as

def fib(self, n)

Then you may call the method like:

f = Fibonacci()
f.fib(5)

The self parameter is referring to the class object, so that you may use self attributes in the class method, in you case, you may have

def fib(self, n):
        a, b = 0, 1
        while a < n:
            print(a, end=' ')
            a, b = b, a + b
        print()
        print(self.title)
  • thanks! i tried to call a function like "Fibonacci.fib(100)" and got an error like TypeError: fib() missing 1 required positional argument: 'n'. any idea? – Doyeon Dec 03 '20 at 05:14
  • You have to initialize the class object, Fibonacci().fib(100) will do. – Clement Wong Dec 03 '20 at 05:19
0

That's more likely a warning rather than an error. And the warning is saying that you're declaring a method as a part of the class but it's not really bound to any object(missing self). If you're doing that on purpose, that means you ought to use static methods.

So you could either go ahead and add self to both those functions like has been suggested in the other answer, or you could use static methods

class Fibonacci:
    def __init__(self, title="fibonacci"):
        self.title = title

    @staticmethod
    def fib(n):
        a, b = 0, 1
        while a < n:
            print(a, end=' ')
            a, b = b, a + b
        print()

    @staticmethod
    def fib2(n):
        result = []
        a, b = 0, 1
        while a < n:
            result.append(a)
            a, b = b, a + b
        return result

And the way you'd call it would be Fibonacci.fib(your_num_here)

Ayush
  • 1,510
  • 11
  • 27