PDA

View Full Version : Basic python question



WinterMadness
October 16th, 2011, 12:34 AM
this has got me pretty confused.

im looking at code here, and im tryng to translate it to another language and i cant figure out what this syntax means.


if not (self.neighbor.advance() == True

self is like this
neighbor is a variable used in the constructor as far as I can tell, and advance is a function that gets recursed.

How on earth can a variable like neighbor use advance() like that, what does it even mean?

Bachstelze
October 16th, 2011, 12:49 AM
Looks like you are not familiar with object-oriented programming. Read up on it.

WinterMadness
October 16th, 2011, 12:58 AM
No, im familiar with OOP. I am not familiar with how a variable can use a function in that way. Ive never seen it done that way, ive been programming for a few years.

if I were to take a variable of the type int and use a dot after it, I get functions associated with the int type, not functions that are part of the current class. Same concept. I want to know what python is actually doing here. Please dont give dismissive answers. They dont help anyone, ever.

Keep in mind advance() is a function IN THE CLASS from THAT code, hence the "recursion" I talked about.

Bachstelze
October 16th, 2011, 01:06 AM
Show the code then, because what you say is not very clear...

WinterMadness
October 16th, 2011, 01:14 AM
Constructor of the class(I assume):

def __init__(self, col, neigh):
self.row = 1
self.column = col
self.neighbor = neigh


Code that exists in the class

def advance(self):
# moves queen one row ahead, or fails and resets
if self.row < numcolumns:
self.row = self.row + 1
return self.findSolution()

if not (self.neighbor.advance() == True):
return False

Where the constructor code is, is in fact the first time the term neighbor comes up. the self.neighbor.advance() is probably some kind of short hand(im guessing). But im not familiar with python much at all.

Bachstelze
October 16th, 2011, 01:41 AM
The constructor assigns to self.neighbor the value of the parameter neigh. neigh can be anything, so if it is of some type that provides an advance() method, self.neighbor.advance() will invoke that method.

WinterMadness
October 16th, 2011, 01:58 AM
okay, so it seems that its probably not recursing there, and the datatype has a function associated with it known as advance()

any idea what type that may be? Im looking on google and im not having much luck, thanks for the help as well

I use the type function to figure out and it just says <type 'instance'>

cgroza
October 16th, 2011, 02:01 AM
You need to dig deeper in that code.
I would start by looking for a place where the class that receives the argument is instantiated. I would expect its argument instantiation to be somewhere around there.

WinterMadness
October 16th, 2011, 02:22 AM
def canAttack(self, testRow, testColumn):
# supposed to return true if I can be attacked by my neighbor
if self.row == testRow:
# print "I can attack my own row"
return True

columnDiff = testColumn - self.column
if ((self.row + columnDiff) == testRow) or ((self.row - columnDiff) == testRow):
# print "I can attack on the diagonal"
return True

result = self.neighbor.canAttack(testRow, testColumn)
return result

Theres more code. Keep in mind, every bit of code ive shown thus far is part of the same class.

so as far as instantiation goes, it hasnt been yet, and the constructor code was already posted. Sorry, but I dont know python.

cgroza
October 16th, 2011, 02:38 AM
That code does not instantiate any class.

WinterMadness
October 16th, 2011, 02:45 AM
I didnt say it did... Im just asking what its doing because I am translating the code to another language, and im stuck on the syntax of python.

JDShu
October 16th, 2011, 02:57 AM
What do you not understand? It looks like neighbor is an attribute of whatever that class is. An attribute can be anything, an int, a string, or an instantiation of a user defined class. If that attribute happens to have a method, then you can call that method.

WinterMadness
October 16th, 2011, 03:08 AM
What do you not understand? It looks like neighbor is an attribute of whatever that class is. An attribute can be anything, an int, a string, or an instantiation of a user defined class. If that attribute happens to have a method, then you can call that method.

The function its calling is part of the class that im trying to translate.

example:


class queen:

def __init__(self, col, neigh):
self.row = 1
self.column = col
self.neighbor = neigh

def canAttack(self, testRow, testColumn):
# supposed to return true if I can be attacked by my neighbor
if self.row == testRow:
# print "I can attack my own row"
return True

columnDiff = testColumn - self.column
if ((self.row + columnDiff) == testRow) or ((self.row - columnDiff) == testRow):
# print "I can attack on the diagonal"
return True

result = self.neighbor.canAttack(testRow, testColumn)
return result

I dont understand the syntax of whats happening. As you can clearly see, there is a function called canAttack. There is a variable IN THE EXACT SAME CLASS AS THE ONE IM WORKING ON called neighbor, thats called in the constructor.

How can a variable call a function like that?

I dont know how else to put this. There is some kind of recursion going on, and ive never seen code that allowed a variable to call a function that does not belong to it.

So to make this even clearer, lets assume that everything im talking about is in the same class(because thats how it is in ALL the code ive shown), lets say you had a variable of type int named x. you also programmed your OWN function that does something called getSomething()

Essentially what I see happening is x.getSomething();

the function getSomething does NOT belong to x, and in most languages this code couldnt possibly work. However, I can assure you the code(THE PYTHON CODE) im looking at not only compiles but actually works.
So again, how can neighbor call the function canAttack()?

Vaphell
October 16th, 2011, 03:12 AM
oh man

somewhere in the program you most likely have

neigh = SomeClass( args )
q = Queen( a, b, neigh )

so the neighbor variable you see inside the queen class is a reference to a different object, that happens to have canAttack() and advance() methods. My bet is that it could be any chess piece though i don't understand why Queen doesn't inherit from some abstract ChessPiece class.

WinterMadness
October 16th, 2011, 03:16 AM
If this helps, ill just post the entire code. I didnt want to do this because I thought people would assume that I was just looking for them to do everything for me, when im just looking for one thing. Thankfully the code isnt very long.


import sys

# definitions needed for older python versions
# True = 1
# False = 0

class queen:

def __init__(self, col, neigh):
self.row = 1
self.column = col
self.neighbor = neigh

def canAttack(self, testRow, testColumn):
# supposed to return true if I can be attacked by my neighbor
if self.row == testRow:
# print "I can attack my own row"
return True

columnDiff = testColumn - self.column
if ((self.row + columnDiff) == testRow) or ((self.row - columnDiff) == testRow):
# print "I can attack on the diagonal"
return True

result = self.neighbor.canAttack(testRow, testColumn)
return result

def advance(self):
# moves queen one row ahead, or fails and resets
if self.row < numcolumns:
self.row = self.row + 1
return self.findSolution()

if not (self.neighbor.advance() == True):
return False

self.row = 1
# print "Q back at (" + str(self.column) + "," + str(self.row) + ")"
result = self.findSolution()
return result


def findSolution(self):
# searches for solution

# print "trying (" + str(self.column) + "," + str(self.row) + ")"
while self.neighbor.canAttack(self.row, self.column):
if not self.advance():
return False
return True


def display(self):
# prints out position of all queens

self.neighbor.display()
print "Q at (" + str(self.column) + "," + str(self.row) + ")"

class sentinelqueen:

def canAttack(self, testRow, testColumn):
# print "Sentinel queens cannot attack"
return False

def findSolution(self):
return True

def advance(self):
# print "Sentinel queens cannot advance"
return False

def display(self):
pass

# MAIN PROGRAM CODE

if len(sys.argv) > 1:
numcolumns = int(sys.argv[1])
else:
numcolumns = 8

Q = sentinelqueen()

for x in range (1,numcolumns+1):
Q = queen(x, Q)
result = Q.findSolution()

if result:
Q.display()
else:
print "No solution."

JDShu
October 16th, 2011, 03:16 AM
There is no recursion. The neighbor attribute is just another piece on the chessboard. That piece happens to also have a method named canAttack.

satsujinka
October 16th, 2011, 03:17 AM
I didnt say it did... Im just asking what its doing because I am translating the code to another language, and im stuck on the syntax of python.

It's not the python syntax that's screwing with you. The person who wrote that code is just completely insane.

WinterMadness
October 16th, 2011, 05:43 AM
Well, I figured it out. Thanks to everyone who tried to help.

The lack of specified types in Python can really be confusing sometimes. Later on in the code, the queen type was instantiated and actually passed its self as an argument, and that cleared everything up for me. Weird.

nvteighen
October 16th, 2011, 04:46 PM
The lack of specified types in Python can really be confusing sometimes. Later on in the code, the queen type was instantiated and actually passed its self as an argument, and that cleared everything up for me. Weird.

This wouldn't have been really better if it was C or C++... I mean, you'd know the name of the type, but you'd still have to backtrack what neigh is supposed to be.

Today's lesson: people should document their code, specially if they're handing it out to other people or publishing it somewhere.

ofnuts
October 16th, 2011, 07:56 PM
This wouldn't have been really better if it was C or C++... I mean, you'd know the name of the type, but you'd still have to backtrack what neigh is supposed to be.

Today's lesson: people should document their code, specially if they're handing it out to other people or publishing it somewhere.Somehow, you always hand out the code to yourself, 6 months later, and lacking sleep, and so should comment accordingly all these clever things that are written when your brain in in top gear.