Re: how can I use a callable object as a method
Piotr Sobolewski <[EMAIL PROTECTED]> writes: > I would like to use a callable object as a method of a class. So, when I > have such normal class: > > I want to change it to something like that: > > class add: > def __call__(self, another_self): > return another_self.version > > class f: > version = 17 > a = add() > > f1 = f() > print f1.a() > > However, the second version does not work. I think I understand why. That's > because "a" inside f1 is not a function (but an object). So f1.a is not a > method. So when I do f1.a(), the implicit argument self is not passed. > > Q1: Am I right? Is this the problem? > Q2: What can I do to make it work? Use the right argument for the call. Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> class add: def __call__(self, another_self): return another_self.version ... ... ... >>> class f: version = 17 a = add() ... ... ... >>> f1 = f() >>> f1 <__main__.f instance at 0x00A805D0> >>> f1.a <__main__.add instance at 0x00A80DA0> >>> f1.a() Traceback (most recent call last): File "", line 1, in TypeError: __call__() takes exactly 2 arguments (1 given) >>> f1.a(f1) 17 >>> HTH -- http://mail.python.org/mailman/listinfo/python-list
Re: Explanation about pickle module
"raghu" <[EMAIL PROTECTED]> writes: > can any one explain about pickle i read in the book but they have not > provided any example for that so please explain with a simple example > >>> class Foo(object): ... def __init__(self): ... self.bar = 1 ... >>> import pickle >>> a = Foo() >>> pickle.dumps(a) "ccopy_reg\n_reconstructor\np0\n(c__main__\nFoo\np1\nc__builtin__\nobject\np2\nNtp3\nRp4\n(dp5\nS'bar'\np6\nI1\nsb." >>> b = >>> pickle.loads("ccopy_reg\n_reconstructor\np0\n(c__main__\nFoo\np1\nc__builtin__\nobject\np2\nNtp3\nRp4\n(dp5\nS'bar'\np6\nI1\nsb.") >>> b.bar 1 >>> b <__main__.Foo object at 0x402ae68c> >>> There is also pickle.dumps and pickle.loads to work directly with files. Further there is module cpickle which offers the same functionality as pickle but which is faster. Bye -- Marco Wahl http://visenso.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Class and instance question
rzed <[EMAIL PROTECTED]> writes: To simplify take > class T(object): > def __new__(self): > self.a = 1 and t = T() and then you get >>> print t.a > Traceback (most recent call last): > File "", line 1, in > AttributeError: 'NoneType' object has no attribute 'a' While T.a is 1. > So what the heck is 'T'? It seems that I can't instantiate it or > derive from it, so I guess it isn't a proper class. But it's > something; it has an attribute. What is it? I don't know. > How would it be used > (or, I guess, how should the __new__() method be used)? Any hints? The __new__ method should return the class. In your case return is None. Further the parametername for the __new__ method should be better cls to have a distinction to the usual self for instances. See http://www.python.org/doc/2.4.4/ref/customization.html Best wishes -- http://mail.python.org/mailman/listinfo/python-list
Re: unit test for a printing method
"Fredrik Lundh" <[EMAIL PROTECTED]> writes: >>> >>> why are you trying to reinvent doctest ? >> >> The OP asked for unit test. This could be read that >> the OP wants to use module unittest. > > http://docs.python.org/lib/doctest-unittest-api.html Ahh, that's good to know that doctests can be integrated into unittest suites. Thank you for the information -- http://mail.python.org/mailman/listinfo/python-list
Re: unit test for a printing method
> [OP] What is the proper way to test (using unit test) a method that print > information? > [...] Fredrik Lundh <[EMAIL PROTECTED]> writes: > > Scott David Daniels wrote: > >> For silly module myprog.py: >> def A(s): >> print '---'+s+'---' >> in test_myprog.py: >> import unittest >> from cStringIO import StringIO # or from StringIO ... > > why are you trying to reinvent doctest ? The OP asked for unit test. This could be read that the OP wants to use module unittest. -- http://mail.python.org/mailman/listinfo/python-list
Re: dicts vs classes
"Guyon Morée" <[EMAIL PROTECTED]> writes: > I'm using simple classes as a container of named values and I'm > instantiating a lot of them in a very short time. > > i was wondering if there is any benefit in using dicts instead from a > performance/memory usage point of view? I recommend you to measure the time and memory usage for the two alternatives. That could give you the answer you want. HTH -- Marco Wahl http://visenso.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Detupleize a tuple for argument list
> Marco Wahl enlightened us with: > > >>> foo(t) > > Traceback (most recent call last): > >File "", line 1, in ? > > TypeError: foo() takes exactly 2 arguments (1 given) > > Call foo(*t) Thank you very much Luke Plant, Steven D'Aprano and Sybren Stuvel. This was exactly what I was looking for. I'm happy now. ;-) -- Marco Wahl http://visenso.com -- http://mail.python.org/mailman/listinfo/python-list
Detupleize a tuple for argument list
Hi, I want to give a tuple to a function where the function expects the respective tuple-size number of arguments. The following session illustrates what I want to do and the respective failure. Python 2.4.1 (#7, Aug 3 2005, 14:55:58) [GCC 3.3.1 (SuSE Linux)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> def foo(a, b): print a, b ... >>> t = (1, 2) >>> def foo(a, b): print 'a == %s, b == %s' % (str(a), str(b)) ... >>> foo(1, 2) a == 1, b == 2 >>> foo(t) Traceback (most recent call last): File "", line 1, in ? TypeError: foo() takes exactly 2 arguments (1 given) >>> One way to do what I want is--of course--to call foo(t[0], t[1]). My actual question is if there is a smarter way to do it. The situation for me is that I take the functions from a library that I cannot modify. On the other side in my code I use the tuples. Best wishes -- Marco Wahl http://visenso.com -- http://mail.python.org/mailman/listinfo/python-list
Re: a class variable question
Hi, just some lines added below. hth [EMAIL PROTECTED] wrote: > hi > i have define a class like this > > class A: > _var1 = 0 > def __init__(self): > ## some initialization > self.func1() > > def func1(): self > . > _var1 = 1 A._var1 = 1 > > > def getvarValue(self): > return _var1 return A._var1 > I wanted var1 to be "global" inside Class A. > when i do > > AnInstance = A() > AnInstance.getvarValue() > > it gives me 0. It should be 1. What am i doing wrong here..thanks -- http://mail.python.org/mailman/listinfo/python-list
Re: global name is not defined - error
"a" <[EMAIL PROTECTED]> writes: > What I want > --- > I want to create a list of items from a function operating on an array > of strings Ok. > What I did > - > list=["s0","s1","s2"] > l=len(list) > for i in range(l): > d_list[i]=f.do(list[i]) > print d_list[i] Aha! > Error: > -- > global name 'd_list' is not defined > Pythonc:\test.py in newClass, line 30 Just as the error message tells: 'd_list' is not defined which is an error. Try list=["s0","s1","s2"] d_list = [] l=len(list) for i in range(l): # d_list[i]=f.do(list[i]) d_list.append(f.do(list[i])) print d_list[i] This is just one suggestion there may be more elegant ways. Have you heard about list comprehension? HTH -- http://mail.python.org/mailman/listinfo/python-list
Re: Where is documentation for +=
> > [...] I can't find any documentation for +=. > > Any hints? > http://docs.python.org/ref/augassign.html That's it. Thank you very much. -- http://mail.python.org/mailman/listinfo/python-list
Where is documentation for +=
Hello, by accident I found that += exists in python. E.g. >>> a = 0 >>> a += 42 >>> a 42 >>> a += 0.42 >>> a 42.422 >>> But I can't find any documentation for +=. Any hints? Best wishes -- http://mail.python.org/mailman/listinfo/python-list
Re: a question on re
Hi, > i tried to search 2 patterns > > pat1 = re.compile("blah") > pat2 = re.compile("blah2") > > > if i do > if re.findall(pat1,something) and re.findall(pat2,something): >do something > > if does not work > > but when i do a nest if, > > if re.findall(pat1,something) : >if re.findall(pat2,something): >do something > > it works.. > > please advise on why the first code doesnt work. it's not perfectly clear to me what you mean with 'doesnt work'. The two if parts > if re.findall(pat1,something) and re.findall(pat2,something): >do something and > if re.findall(pat1,something) : >if re.findall(pat2,something): >do something look equivalent to me. Could you please reveal your value for text-string 'something'? Further I suggest to replace the 'do something'-parts with 'print 42'. Best wishes Marco -- http://mail.python.org/mailman/listinfo/python-list