1

i have a list of items. i then have a function to find an item in there.

def find(self, runners):
    for runners in self._runners:
        if runners.name == name:
            return runners

so when i type runnersworld.find(peter) it should bring back a print statement such as "record found or record not found. each record has a runnerid and a runner name. but when i run this function i get a global variable name it not defined. the two fines i have are:

RunnerClass

class Runner(object):
def __init__ (self, runnerid, name):
    self._runnerid = runnerid
    self._name = name

@property
def runnerid(self):
    return self._runnerid

@property
def name(self):
    return self._name

@name.setter
def name(self, name):
    self._name = name

def get_fee(self, basicfee, moneyraised):
    raise NotImplementedError("AbstractMethod")        

def __str__(self): 
    rep = "Runner: " + self._runnerid + " " + self._name + "\n"
    return rep

Club Class

class Club (object):
def __init__(self, clubid, name):
    self._clubid = clubid
    self._name = name
    self._runners = []

@property
def clubid(self):
    return self._clubid

@property
def name(self):
    return self._name

@name.setter
def name(self, name):
    self._name = name

@property
def runners (self):
    return self._runners

@runners.setter
def runners(self, runners):
    self._runners = runners

def add(self, runners):
    if runners not in self._runners:
        self._runners.append(runners)

def remove(self, runners):
    if runners in self._runners:
        self._runners.remove(runners)

def find(self, runners):
    for runners in self._runners:
        if runners.name == name:
            return runners    

def __str__(self):
    rep = "Club: " + self._clubid + " " + self._name + "\n"
    for runner in self._runners:
        rep += str(runner)
    return rep

i have edited my find function to:

def find(self, name):
    for runners in self._runners:
        if runners.name == name:
            print("Runner found in Records")
        else:
            print("Runner not found in record") 

it seems to work but when i try to run runnersworld.find(peter) and runners4life.find(peter) i get:

Runner not found in record

Runner not found in record

Runner not found in record

Runner not found in record

Runner not found in record

Runner not found in record

but there is defininatly a peter in runners4life

Ross
  • 303
  • 1
  • 4
  • 19
  • 2
    Can you show the trace-back? – Kobi K Dec 26 '13 at 11:24
  • what do you mean by trace back. im new to python sorry. – Ross Dec 26 '13 at 11:26
  • 1
    When you have an error the interpreter will raise an exception that explain what went wrong, You have to know it since you said `but when i run this function i get a global variable name it not defined.` – Kobi K Dec 26 '13 at 11:30
  • You should see something beginning with `Traceback (most recent call last):`. If you are running the program the intepreter window may close before you can read it. Try running the program from whichever IDE you are using. – rlms Dec 26 '13 at 11:32
  • ooh ok thank. no worries Danstahr fix it that issue. but thank you anyway – Ross Dec 26 '13 at 11:34
  • 1
    On another topic, although it is usual practice to define getters and setters in languages such as C#, it is usually better to just access attributes directly. See [this](http://stackoverflow.com/questions/1641219/does-python-have-private-variables-in-classes) answer for an explanation. – rlms Dec 26 '13 at 11:37
  • 1
    Totally unrelated but when you have a property that just get and set the same attribute without any computation, you can as well replace it with a plain attribute. That's the whole point of properties (and other coumputed attributes) FWIW : being able to start with a plain attribute and only turn it in a computed one if and when necessary. – bruno desthuilliers Dec 26 '13 at 11:39

1 Answers1

5

In the following piece of code :

def find(self, runners):
    for runners in self._runners:
        if runners.name == name:
            return runners    

there's no variable name defined. You probably mean

def find(self, name):
    for runners in self._runners:
        if runners.name == name:
            return runners
Danstahr
  • 4,190
  • 22
  • 38