0

I have been coding in Java for years and my school here teaches Python, and I'm having a lot of trouble with it. My professor has asked us to create Connect4 in python and to start with these two methods:

def __init__( self, width, height ): 
self.width = width 
self.height = height 
self.data = [] # this will be the board 

for row in range( self.height ): 
    boardRow = [] 
    for col in range( self.width ): 
        boardRow += [' '] 
    self.data += [boardRow] 

def __repr__(self): 
#print out rows & cols 
    s = '' # the string to return 
    for row in range( self.height ): 
        s += '|' # add the spacer character 
        for col in range( self.width ): 
            s += self.data[row][col] + '|' 
        s += '\n' 

    #print out separator 
    #your code here 

    # print out indices of each column 
    # using mod if greater than 9, 
    # for spacing issues 
    #your code here

    return s # return it 

I don't understand the self argument, I've read lots about it and nothing seems to make sense to me. I was hoping someone could give me a little explanation of what self is doing in this function, and why my professor is saying that the first function only two arguments are given but there are clearly three.

Any help/explanations would be greatly appreciated!

Here is where I got those functions.

Giupo
  • 413
  • 2
  • 9
  • 3
    Have you looked at this? http://stackoverflow.com/q/2709821/748858 – mgilson Dec 04 '13 at 22:07
  • `self` is the instance of your class. It is implicitly (or explicitly, depending on your viewpoint) passed as the first argument to every method. It is just like `this` in java, but you explicitly have to name it as the first argument to every method. – roippi Dec 04 '13 at 22:09
  • I've edited your code (checkit, I hope it's correct ;) because it was wrongly indented and indents with Python are extremely important ;) – Giupo Dec 04 '13 at 22:11
  • Not to be rude but I think it was actually right before, i'm getting indentation errors now :s Also yes i've read through the 'self' explained thing but i guess i'm still not understanding or missing something.. I'll do some more research. – user3067803 Dec 04 '13 at 22:20
  • `__init__ ` still is indented wrong ... he fixed some of your code ... (the indentation was probably fine for your system... but not for how stack overflow formatted it ... – Joran Beasley Dec 04 '13 at 22:21
  • If you can tell us what specifically you don't understand from the "self explained" link, that would make a good question. In a single sentence, Java magically allows you to use `this` in an instance method, whereas Python requires you to pass `self` so you can use it, because ["Explicit is better than implicit"](http://www.python.org/dev/peps/pep-0020/) – Prashant Kumar Dec 04 '13 at 22:34
  • What I don't understand is say I try and use the first function which takes 2 arguments, 3 with self included, what do i type in? If i type this in the shell "__init__(6,7)" it tells me it needs 3 arguments, but I don't think I'm supposed to be setting self to an int am I? – user3067803 Dec 05 '13 at 22:07

1 Answers1

1

when calling a method on a class it passes the class instance as the first argument to the method , typically programmers name this argument self, but it can be named whatever

class CheckMeOut:
    def class_func(self):
        print self

    def class_func1(s):
         print s

    def class_func2(xyz):
         print xyz

c = CheckMeOut() #create Instance
c.class_func()
c.class_func1()
c.class_func2()

or just look at the link mgilson supplied

Joran Beasley
  • 110,522
  • 12
  • 160
  • 179