Johnnerz
Johnnerz

Reputation: 1425

Run multiple functions in a for loop python

Here is the start of my program. I want a lot of the functions to be inside the for loop as seen in the 3rd function here. How do I go about this?

#!/usr/bin/env python

from rdflib import URIRef, Graph
from StringIO import StringIO
import subprocess as sub

class Wordnet():

    def __init__(self, graph):
        graph = Graph()

    def process_file(self, file):
        file = open("new_2.txt", "r")
        return file

    def line_for_loop(self, file):
        for line in file:

    def split_pointer_part(self, before_at, after_at, line):
        before_at, after_at = line.split('@', 1)
        return before_at, after_at    

    def split_word_part(self, word_part, line):
        word_part = line.split()
        return word_part

Is it just a matter of indenting everything else in the for loop or is it when the function are called that the loop has to be defined?

How does one go about calling multiple functions as part of a program? I am new to python and i don't really know.

Upvotes: 0

Views: 5423

Answers (2)

Daniel Roseman
Daniel Roseman

Reputation: 600049

There's no program here. Classes by themselves don't do anything. You need to instantiate the class, then call one of its methods (which is the correct term for what you seem to be calling "processes"). So, at the end of this file, you might do:

wordnet = Wordnet()
my_file = wordnet.process_file()
wordnet.line_for_loop(my_file)

Inside one method, you can call another: so for your loop, you would do:

def line_for_loop(self, file):
    for line in file:
        self.my_method_1()
        self.my_method_2()

There are some other issues with your code. For example, in the __init__ method, you define a graph local variable, but never do anything with it, so it is not stored anywhere. You need to store variables on self for them to become instance properties:

def __init__(self):
    self.graph = Graph()

Also, you seem to be confused about when to pass parameters to functions. Twice (in __init__ and process_file) you accept a parameter, then override it inside the method with a local variable. If you're defining the variable in the function, you shouldn't pass it as a parameter.

Note that, as I've had occasion to say before, Python is not Java, and doesn't always require classes. In this case, the class is not contributing anything to the program, other than as a holder for methods. In Python, you would normally just use functions inside a module for that.

Upvotes: 2

Process isn't the proper term to use. Those are better known as functions or methods. As far as Python loops go, indentation is important. You do need to indent.

def line_for_loop(self, file):
    for line in file:
        process_file("example_file_name.txt")
        split_pointer_part(0, 10, "some test string")

You should make the function calls from inside the loop. The example code above may not be the exact solution for you code, but it should be sufficient enough to answer your question.

Upvotes: 0

Related Questions