tags:

views:

99

answers:

2

i have been asked to write a function which should be called in this way

foo("Hello")

This function also has to return values in this way:

[Hello( user = 'me', answer = 'no', condition = 'good'),
 Hello( user = 'you', answer = 'yes', condition = 'bad'),
]

the task has clearly asked to return string values. can anyone understand the purpose of this task in concepts of Python and help me on this? would you please give me a code sample?

A: 

Functions

Lists

Classes

Create a class that has the desired attributes, then return a list of instances from the function.

Ignacio Vazquez-Abrams
A: 

It could be something like this:

class Hello:
    def __init__(self, user, answer, condition):
        self.user = user
        self.answer = answer
        self.condition = condition

def foo():
    return [Hello( user = 'me', answer = 'no', condition = 'good'),
    Hello( user = 'you', answer = 'yes', condition = 'bad'),
    ]

Output of the foo function:

[<__main__.Hello instance at 0x7f13abd761b8>, <__main__.Hello instance at 0x7f13abd76200>]

Which is a list of class instances (objects).

You can use them this way:

for instance in foo():
    print instance.user
    print instance.answer
    print instance.condition

Which gives:

me
no
good
you
yes
bad
kolobos