Drickken Drickken - 7 months ago 8
Python Question

How to access functions that use self outside of class

I have been trying to access a function made within a class outside of said class.
If self is not one of the function's parameters it works just fine, as in:

class Dog():

def Bark():
print("woof")

Marley = Dog

Marley.Bark()


But If I add self to Bark() as in:

class Dog():

def Bark(self):
print("woof")

def Bark_lots(self):
for I in range(3):
self.Bark()

Marley = Dog

Marley.Bark_lots()


Then python throws a type error:

TypeError: Bark_lots() missing 1 required positional argument: 'self'

Everything that I've searched up just talks about why self is needed and what it does. I am clearly missing something, and from past experience it is probably simple. Any help on this issue would be much appreciated.

(Also I am new to stack exchange/ stack overflow, If I broke any of the rules please alert me to where and I will fix it.)

Answer

After the call сlass you need to put parenthesis (Dog(), not Dog):

Marley = Dog()
Marley.Bark()