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

Dave Angel d at davea.name
Mon May 7 21:07:46 CEST 2012

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.

> I use python3
> Thanks in advance,
> Xan.



More information about the Tutor mailing list