开发者

How to start a Python script several functions in

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 printAdd开发者_如何学运维ress() down the the end of the script?

Thanks


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.


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()


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


A more flexible way of handling command line options is to use the 'optparse' module. Check out the reference: http://docs.python.org/library/optparse.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜