Re: Promoting Python

2016-04-06 Thread Rolf Camps



Op 07-04-16 om 00:03 schreef Marko Rauhamaa:

Once you look up an object method, it doesn't have a self argument. The
self argument is cooked up for the benefit of the function definition in
the class.

IOW, if I have this class:

 class A:
 def f(self):
 print("f")

and this object:

 a = A()

then,

 a.f

is a function that doesn't have a self argument. That function is
generated on the fly, and it delegates to A.f, providing it with self
argument.

a.f is not a function, A.f is a function.
a.f is an instance method. The function is not generated on the fly, 
when the method is called, it calls the function A.f with an extra 
argument __self__ (the instance a) inserted before the argument list the 
instance method was called with.

So calling a.f() is a convenient way to write A.f(a)


However, a.f itself is just a function that doesn't accept any
arguments.
As i wrote, a.f is an instance method not a function. It accepts any 
argument you trow at it, insert 'a' in front of the argument list and 
calls the function A.f with this new argument list.


Defining:

 def f():
 print("f")
 a.f = f

simply short-circuits the attribute lookup process; no need to consult
the class when the attribute (method!) is right there in the object's
dict.
Here a.f is a function so Python will treat is as a function. When you 
call it, the lookup process will find f in the class instance 
dictionary, discover it's a function and run it with the arguments 
provided. If you would like to use  attributes of the class A or of the 
instance 'a' in f, you would need to hard code it in the function since 
the function f does not know it was called from 'a', an instance of the 
class A.



Marko

--
https://mail.python.org/mailman/listinfo/python-list


Re: deque is not a subclass of Sequence.

2016-04-07 Thread Rolf Camps



On 2016-04-07 11:12, Peter Otten wrote:

Antoon Pardon wrote:


Second tryal, I hope the formatting doesn't get messed up now

Playing around with the collections and collections.abc modules in
python3.4 I stumbled upon the following:


from collections.abc import Sequence
from collections import deque
isinstance(list(), Sequence)

True

isinstance(deque(), Sequence)

False

This seems strange to me. As far as I understand, the
documentation indicates there is no reason why deque
shouldn't be a subclass of Sequence.

Am I missing something or can this be considered a bug?

from collections import deque
from collections.abc import Sequence
[name for name in set(dir(Sequence)) - set(dir(deque)) if not

name.startswith("_")]
['index']

So the index() method seems to be what is missing.


The index method was added to the deque object in Python 3.5.

Python 3.5.1 (default, ...)
[GCC ...] on linux
>>> import collections
>>> isinstance(collections.deque(), collections.abc.Sequence)
True

--
https://mail.python.org/mailman/listinfo/python-list


Re: what does 'a=b=c=[]' do

2011-12-22 Thread Rolf Camps
alex23 schreef op wo 21-12-2011 om 16:50 [-0800]:
> On Dec 22, 8:25 am, Eric  wrote:
> > This surprises me, can someone tell me why it shouldn't?  I figure if
> > I want to create and initialize three scalars the just do "a=b=c=7",
> > for example, so why not extend it to arrays.
> 
> The thing to remember is that everything is an object, and that it's
> better to think of variables as labels on an object.
> 
> So: a=b=c=7 means that _one_ integer object with the value of 7 can be
> referenced using any of the labels a, b or c. x=y=z=[] means that
> _one_ empty list can be referenced using x, y or z.
> 
> The difference is that the value of a number object _cannot be
> changed_ ('immutable') while a list can be modified to add or remove
> items ('mutable'). a=10 just reassigns the label a to an integer
> object of value 10. x.append("foo") _modifies_ the list referred to by
> x, which is the same list known as y & z.
> 
> > Also, is there a more pythonic way to do "x=[], y=[], z=[]"?
> 
> I'd say that _is_ the most pythonic way, it's very obvious in its
> intent (or would be with appropriate names). If it bothers you that
> much:
> 
> def listgen(count, default=[]):
> for _ in xrange(count):
> yield default[:]
> 
> x, y, z = listgen(3)
> 


I'm afraid it's dangerous to encourage the use of '[]' as assignment to
a parameter in a function definition. If you use the function several
times 'default' always points to the same list. 

>>> def return_list(list_ = []):
>>> return list_
>>> a_list = return_list()
>>> a_list
[]
>>> a_list.append(3)
>>> a_list
[3]
>>> b_list = return_list()
>>> b_list
>>> [3]   # !!??

>>> def return_list():
>>> return []
>>> a_list = return_list()
>>> a_list
[]
>>> a_list.append(3)
>>> a_list
[3]
>>> b_list = return_list()
>>> b_list
>>> []# OK!

I only use python3 so I don't know how these things work in other
versions.

No problem in your function since you yield a copy, but I've already
seen long threads about this.

I would change your function to (Python3.x):

def empty_lists(count):
for _ in range(count):
yield []


Regards,

Rolf




-- 
http://mail.python.org/mailman/listinfo/python-list


plotting in python 3

2010-04-06 Thread Rolf Camps
Hi,

What's the easiest way to plot graphs in python 3.x? Ís there a package?
Quality doesn't really matter.

Thanks,

Rolf


signature.asc
Description: Dit berichtdeel is digitaal ondertekend
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: plotting in python 3

2010-04-06 Thread Rolf Camps
Op dinsdag 06-04-2010 om 14:55 uur [tijdzone -0500], schreef Christopher
Choi:
> On Tue, 06 Apr 2010 21:23:34 +0200, Rolf Camps wrote:
> 
> > Hi,
> > 
> > What's the easiest way to plot graphs in python 3.x? Ís there a package?
> > Quality doesn't really matter.
> > 
> > Thanks,
> > 
> > Rolf
> > Hi,
> > 
> > What's the easiest way to plot graphs in python 3.x? Ís there a package?
> > Quality doesn't really matter.
> > 
> > Thanks,
> > 
> > Rolf
> 
> I would like to think you have done some home work yourself 
> but anyways... gnuplot with python seems easy.

> .
> 
> http://gnuplot-py.sourceforge.net/doc/


It was after the homework I asked my question. All plot solutions i
found where for python2.x. gnuplot_py states on its homepage you need a
'working copy of numpy'. I don't think numpy is ported to python 3.x. Or
is it? 
> 
> Chris



signature.asc
Description: Dit berichtdeel is digitaal ondertekend
-- 
http://mail.python.org/mailman/listinfo/python-list