[Tutor] How can I have type "function" in my script?

xancorreu xancorreu at gmail.com
Tue May 8 12:23:46 CEST 2012


Al 08/05/12 01:24, En/na Alan Gauld ha escrit:
> On 07/05/12 21:37, xancorreu wrote:
>
>> This is the code:
>
> OK, But it's not clear from that why you want the type.
> You are not doing anything meaningful with the type.
>
>> class Tag:
>>
>> def __init__(self, nom, tipus, valor):
>> self.nom = nom
>> self.tipus = tipus
>> self.valor = valor
>>
>> def __str__(self):
>> return "Nom: " + str(self.nom) + ", Tipus: " + str(self.tipus) + ",
>> Valor: " + str(self.valor)
>
>
> You store it then print the string version of it.
> Why not just pass a string name?
>
>> def main():
>> a = Tag("descripció", str, "primera tasca")
>> b = Tag("unmes", str, lambda x: x+1)
>> c = Tag("twice", type(lambda: x: x), lambda x: 2*x)
>
>> and it fails here:
>>
>> $ python3 tasques.py
>> File "tasques.py", line 26
>> c = Tag("twice", type(lambda: x: x), lambda x: 2*x)
>> ^
>> SyntaxError: invalid syntax
>
>
> As it says there is a syntax error. Look at your two lambda 
> expressions, the second one has an extra :


Thanks a lot,

It works with

     c = Tag("twice", type(lambda x: x), lambda x: 2*x)


but I want to specify the type of (lambda x: x) **manually** as I do 
with str in b

Thanks


More information about the Tutor mailing list