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

Joel Goldstick joel.goldstick at gmail.com
Mon May 7 21:11:55 CEST 2012


On Mon, May 7, 2012 at 3:07 PM, Dave Angel <d at davea.name> wrote:
> 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:

can a class be defined this way in python 3.x?  I thought it needs a
parent class as a parameter?
>>
>>     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.
>
> --
>
> DaveA
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor



-- 
Joel Goldstick


More information about the Tutor mailing list