views:

100

answers:

3

Hello,

I have a Python script and I want to call it several functions down the script. Example code below:

class Name():

    def __init__(self):
        self.name = 'John'
        self.address = 'Place'
        self.age = '100'

    def printName(self):
        print self.name

    def printAddress(self):
        print self.address

    def printAge(self):
        print self.age

if __name__ == '__main__': 
    Person = Name()
    Person.printName()
    Person.printAddress()
    Person.printage()

I execute this code by entering ./name.py. How could I exectute this code from the function printAddress() down the the end of the script?

Thanks

+1  A: 

I would not recommend you'd actually do this, as it's an endless recursive function this way, but it can be done:)

class Name():

    def __init__(self):
        self.name = 'John'
        self.address = 'Place'
        self.age = '100'

    def printName(self):
        print self.name

    def printAddress(self):
        print self.address
        main()

    def printAge(self):
        print self.age

def main():
    Person = Name()
    Person.printName()
    Person.printAddress()
    Person.printage()

if __name__ == '__main__': 
    main()
extraneon
I do not understand what is answered here. The code maybe is better organized but how it relates with the question and what the question is first of all?.
joaquin
@Joaquin The way I read the question was more like: How do I start the main method again from within printAddress. Which didn't make much sense to me (I commented on the question), but could be answered :)
extraneon
@extraneon: right, it was not easy. at least somebody understood it at last
joaquin
A: 

The line

Person = Name()

indicates that the class's name should actually be Person. Drop the printXXX methods entirely and add a __repr__ method instead, e.g. like

def __repr__(self):
    return "%s\n%s\n%s" % (self.name, self.address, self.age)

Finally, I recommend adding name, address and age to the parameter list accepted by init:

def __init__(self, name, address, age):
    ...

After instantiating a Person object, you can just print it:

person = Person()
print person
jellybean
For use in printing use __str__ not __repr__. __repr__ is used for serialization (although in some cases they can be the same)
Mark
+2  A: 

If you are asking how can you launch your python script and have it start executing at different positions then you will have to launch the script with some information on what you want it to do. The most common way to do this would be to add support for command line arguments.

import sys

if __name__ == '__main__':   

    for arg in sys.argv: 
        print arg

If you were to execute the above script from the command line by itself it would not do anything, but if you launch it with some extra parameters such as

./launch.py my_argument another_argument and_so_on

You will see the script has access to the extra launch arguments through the sys.argv list. Using this, you can check for any passed args on launch and then start executing your script at your desired location.

One example with your script could be as follows

import sys

class Name:

    def __init__(self):
        self.name = 'John'
        self.address = 'Place'
        self.age = '100'

    def printName(self):
        print self.name

    def printAddress(self):
        print self.address

    def printAge(self):
        print self.age


if __name__ == '__main__': 

    Person = Name()

    launchOptions = sys.argv[1:]

    if not launchOptions or 'name' in launchOptions:
        Person.printName()

    if not launchOptions or 'address' in launchOptions:
        Person.printAddress()

    if not launchOptions or 'age' in launchOptions:
        Person.printAge()

The range on the sys.argv[1:] is because the first entry in the sys.argv will be the path to the launched script.

So you could launch this example and get the following results

./launch
John
Place
100

./launch age
100

./launch address
Place

./launch name
John

Now this is just a very basic example. If you are decide to go further in this direction it may be useful for you to read up on pythons getopt module. It's a parser for command line options.

Hopefully I understood the question correctly.

James
After reading the other answers, I'm now unsure what the actual question may be.
James
@jamescg -- No, this is perfect. Sorry for the confusing question but this answer it spot on. Thanks +1
chrissygormley