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

2012-05-07 Thread xancorreu

Hi,

I have this script:

from types import *

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)


I use python3


Thanks in advance,
Xan.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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

2012-05-07 Thread xancorreu

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, 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.


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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

2012-05-08 Thread xancorreu

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
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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

2012-05-08 Thread xancorreu

Al 08/05/12 19:36, En/na Alan Gauld ha escrit:

On 08/05/12 11:23, xancorreu wrote:


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



Unfortunately we don't always get what we want.
You have to use type for functions.

You can use a very simple function, but you need to use type()...

type(lambda : 0)

is about as simple as you can do it with lambda...


A much pain for me. I think there should be a type.function avaliable in 
python, like str, int, etc. exists. I think as python is O-O language 
and have (or at least the page says that) higher-order function, I 
should could type:


isinstance(2, function)

like I do

isinstance(2, int)



Do you understand my reasoning?


However, you still haven't said why you want the type. It's very 
unusual in Python to need explicit type information. If we knew what 
you were trying to do with it we might be able to offer a more 
pythonic way to go about it.





Yes, I know that the pythonic way is to not define types in variables. 
The variables are free while there is no assign to these. I need type 
because I want to implement Tag as triple of name, type and value. I 
want to differentiate between a tag with type str and value "today" and 
a tag with tag with type data and value "today".



That's all.


Thanks for the answer.
Xan.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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

2012-05-08 Thread xancorreu

Al 08/05/12 20:42, En/na Emile van Sebille ha escrit:

On 5/8/2012 11:31 AM xancorreu said...

isinstance(2, function)

like I do

isinstance(2, int)



Do you understand my reasoning?


Nope, but here ya go:

Python 2.6.4rc2 (r264rc2:75497, Oct 20 2009, 02:55:11)
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from types import *
>>> dir()
['BooleanType', 'BufferType', 'BuiltinFunctionType', 
'BuiltinMethodType', 'ClassType', 'CodeType', 'ComplexType', 
'DictProxyType', 'DictType', 'DictionaryType', 'EllipsisType', 
'FileType', 'FloatType', 'FrameType', 'FunctionType', 'GeneratorType', 
'GetSetDescriptorType', 'InstanceType', 'IntType', 'LambdaType', 
'ListType', 'LongType', 'MemberDescriptorType', 'MethodType', 
'ModuleType', 'NoneType', 'NotImplementedType', 'ObjectType', 
'SliceType', 'StringType', 'StringTypes', 'TracebackType', 
'TupleType', 'TypeType', 'UnboundMethodType', 'UnicodeType', 
'XRangeType', '__builtins__', '__doc__', '__name__', '__package__']

>>> def test():pass
...
>>>
>>> function=FunctionType
>>> print isinstance(test,function)
>>>



A BIG thank you.

Thanks,
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor