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

Dave Angel d at davea.name
Mon May 7 23:59:55 CEST 2012


On 05/07/2012 04:37 PM, xancorreu wrote:
> Al 07/05/12 21:07, En/na Dave Angel ha escrit:
>> On 05/07/2012 02:24 PM, xancorreu wrote:
>>> Hi,
>>>
>>> I have this script:
>>>
>>> from types import *
>>>
>> Bad idea.  Once you do that, you can silently overwrite globals in your
>> own module with stuff that the current version of types happens to have
>> in it.  Besides, it then becomes very hard to read your program and
>> figure out which names you really did want to import.
>>
>> If you're just getting one or two names, such as in your case, better
>> just do
>>      import types
>>
>>> 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)
>>>
>>>
>>> def main():
>>>      a = Tag("descripció", str, "primera tasca")
>>>      b = Tag("unmes", str, lambda x: x+1)
>>>      print(a)
>>>      print(b)
>>>
>>> if __name__ == '__main__':
>>>          main()
>>>
>>>
>>> All is ok, but when I substitute b = Tag("unmes", str, lambda x: x+1)
>>> for b = Tag("unmes", function, lambda x: x+1) I receive an error that
>>> function is not globally defined variable. How can I say that function
>>> is the types.function? (the type of lambda x: x+1)
>>>
>> Where's the stack trace and the exact error message?
>>
>> types.function is undefined.  The types module does not expose a name
>> called 'function,' at least not in python 3.2
>>
>> The type of a lambda is<class 'function'>, so it's not clear what you
>> really want.
>>
>> Why don't you show the program as you actually run it (perhaps with both
>> versions of the b= assignment), and the output and stack trace you got.
>> Then explain just what you'd hoped to get, as output.
>>
> 
> This is the code:
> 
> 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)
> 
> class Task:
>     _nombre = 0
> 
>     def __init__(self):
>         self.tags = []
>         Task._nombre = Task._nombre + 1
>         self.num = Task._nombre
> 
> 
>     def __str__(self):
>         return "Número: " + str(self.num) + ", Tags: " + str(self.tags)
> 
> 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)
>     # en comptes de str ha de ser lambda
>     print(a)
>     print(b)
>     print(b.valor(2))
>     t = Task()
>     print("Tasca 1:", t)
>     t2 = Task()
>     print("Tasca 2:", t2)
> 
> if __name__ == '__main__':
>         main()
> 
> 
> 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
> 
> 
> Really, I want to specify "manually" the type of lambda, but it does not
> work. How to do that?
> 
> 
> Thanks,
> 
>>> I use python3
>>>
>>>
>>> Thanks in advance,
>>> Xan.
> 
> 

def main():
    a = Tag("descripció", str, "primera tasca")
    print (a)
    b = Tag("unmes", str, lambda x: x+1)
    print(b)
    c = Tag("unmes", "function", lambda x: x+1)
    print(c)

Does that help?

-- 

DaveA


More information about the Tutor mailing list