Why don't functions take a `self` argument?

519 views Asked by At

I am very new to programming in Python and I just had a question as to why when I call a function it doesn't work. I see the TypeError about die_roll needing two arguments but why isn't that covered by self.result when added to the parenthesis?

import random

def die_roll(self, result):
    self.result = random.randint(1, 10)
    print "Roll the dice, Dennis. "
    print "You have rolled a %s!" % self.result

print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
print ("                  Welcome...                    ")
print ("   TO THE WORST GAME IN THE WORLD!!!!!!!!!!!    ")
print ("   REALLY, IT IS QUITE BAD. YOU'LL SEE......    ")
print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n")

name = raw_input('Welcome to this awful adventure. Please enter your name to get started: \n')

print "\n%s? I don't like that name. \n" % (name)

name1 = raw_input('Try again: \n')

print "\n%s?! Really? That's the best you can do? You know what - just forget it. You will be called Dennis. \n" % (name1)

print "I happen to like Dennis. It's a good name. Regal. It's nice to meet you.... Dennis. \n"

print "You begin your adventure peering into a long, gloomy cave. You move by rolling a 10-sided dice.\n"

print "You will encounter random enemies along the way. If your combined rolls equal 100, you win!\n"

die_roll()
2

There are 2 answers

1
Anand S Kumar On

You are defining your function as

def die_roll(self, result):

This tells python interpreter that die_roll , needs two arguments self and result.

I am guessing you copied this method from within some other class, because self is the normal naming convention for the first parameter to a method in a class, in the later case, self (first parameter) refers to 'this' (from c++ or java) .

From the function body, it seems like you do not need any parameters, maybe you can try -

def die_roll():
    result = random.randint(1, 10)
    print "Roll the dice, Dennis. "
    print "You have rolled a %s!" % result
0
TessellatingHeckler On

Hmm. I'm not sure where your main misunderstanding is, but there are several in your question.

You're not following the flow of the program and where the error comes from:

def die_roll(self, result):
    /stuff here/

/stuff/

print "You will encounter random enemies along the way. If your combined rolls equal 100, you win!\n"

die_roll()  <---  here is where the TypeError about die_roll needing two arguments is triggered

why isn't that covered by self.result when added to the parenthesis?, you ask? Because the error is saying that the parentheses when you call die_roll() aren't following the pattern you set when you made def die_roll(...). You can't avoid that error by doing something at the place where the function is defined. They always need to match in both places - all places. If def die_roll says it needs two parameters, then when you call it you need to give it two parameters.

Another misunderstanding is that you are using def die_roll(self, result) and then self.result - as if the comma and the dot operators are somehow related, or that you need to use these arguments as a way to get or return a result. (They aren't, you don't).

Another is that you are using the words self and result as if Python understands them. They aren't keywords, they have no special meaning in Python.

self has no special meaning in Python, however, it is the standard name people give for one particular variable which is ... unfortunately related to object orientation, something else that can be quite a slippery concept to pickup from scratch.

Your title question Why don't functions need a self argument? can be answered in many ways, all basically unhelpful to you right now. blah blah Classes define objects, they contain things which look exactly like functions but are called methods. Methods need a way to reference the object they are 'in', so the first argument to a method is always the object it is in, and the Python engine supplies the first argument so method calls always look like they don't match up because the definition has one more argument than the call. self is a convention that Python programmers have adopted for naming the variable that receives the object reference, although it's not a special name inherently. But that's all unrelated background, setting up...

Functions don't need self because they aren't 'in' objects, so they have no need for a way to reference the object they are 'in', because they aren't in one.

Yeah that's not very helpful, sorry. The most helpful thing I can say is, write more code, use the interactive interpreter more, explore more, and things will become much clearer.