why python cache the string > 256?

2009-10-26 Thread s7v7nislands
hi all:

test.py
#!/usr/bin/python
a = []
for i in xrange(100):
a.append('a'*500)


$python -i test.py #virt mem 514m in top output
>>del a   #virt mem 510m

why python cache these string?
In source, I see when object size > SMALL_REQUEST_THRESHOLD, python
would use malloc.
also use free() when string refcount == 0.

do I miss somethong?

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


Re: why python cache the string > 256?

2009-10-26 Thread s7v7nislands
On Oct 27, 4:03 am, "Diez B. Roggisch"  wrote:
> s7v7nislands schrieb:
>
>
>
> > hi all:
>
> > test.py
> > #!/usr/bin/python
> > a = []
> > for i in xrange(100):
> >     a.append('a'*500)
>
> > $python -i test.py     #virt mem 514m in top output
> >>> del a                   #virt mem 510m
>
> > why python cache these string?
> > In source, I see when object size > SMALL_REQUEST_THRESHOLD, python
> > would use malloc.
> > also use free() when string refcount == 0.
>
> > do I miss somethong?
>
> http://effbot.org/pyfaq/why-doesnt-python-release-the-memory-when-i-d...
>
> Diez

thanks. but it can't explain why cache string size bigger than
SMALL_REQUEST_THRESHOLD.
In source, It seems only allocate from cache pool when object's size <
SMALL_REQUEST_THRESHOLD (256),
when size > SMALL_REQUEST_THRESHOLD, it will direct use malloc.
see void *PyObject_Malloc(size_t nbytes) in Objects/obmalloc.c

I know the range & xrange's memory use. the int,str object has their
memory manager above Python's object allocator.
but why python don't free big string's memory, which size >
SMALL_REQUEST_THRESHOLD

sorry for poor english.
thanks!
-- 
http://mail.python.org/mailman/listinfo/python-list


s.index(x[, i[, j]]) will change the s ?

2009-09-09 Thread s7v7nislands
hi all:
what is the s.index() mean? does the index() change the s?
In python2.6 doc (6.6.4. Mutable Sequence Types), Note 4:

Raises ValueError when x is not found in s. When a negative index is
passed as the second or third parameter to the index() method, the
list length is added, as for slice indices. If it is still negative,
it is truncated to zero, as for slice indices.

Changed in version 2.3: Previously, index() didn’t have arguments for
specifying start and stop positions.

who can give a example?  and why the s.remove() also point to note 4?
Is the document wrong?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: s.index(x[, i[, j]]) will change the s ?

2009-09-09 Thread s7v7nislands
Thanks for your reply! Sorry for my poor english!

On Sep 10, 12:33 pm, Chris Rebert  wrote:
> On Wed, Sep 9, 2009 at 9:00 PM, s7v7nislands wrote:
> > hi all:
> >    what is the s.index() mean? does the index() change the s?
>
> It tells you the index of the first instance of the given element in
> the sequence. Or, to quote the docs:
>     s.index(x[, i[, j]]) --- return smallest k such that s[k] == x and
> i <= k < j
>
> No, .index() does not modify the sequence itself.

I known index() does not modify the sequence itself. my question is so
why the doc put the index() method in the mutable sequence types list?
>
> >    In python2.6 doc (6.6.4. Mutable Sequence Types), Note 4:
>
> > Raises ValueError when x is not found in s. When a negative index is
> > passed as the second or third parameter to the index() method, the
> > list length is added, as for slice indices. If it is still negative,
> > it is truncated to zero, as for slice indices.
>
> > Changed in version 2.3: Previously, index() didn’t have arguments for
> > specifying start and stop positions.
>
> Nothing in the above says anything about modifying a sequence...
When a negative index is passed as the second or third parameter to
the index() method, the list length is added, as for slice indices.
I don't understand the mean.  the list length is added, why? if it
changed, the original will change ?
>
> > who can give a example?  and why the s.remove() also point to note 4?
>
> Because it has the same behavior when the item is not present in the sequence.
>
> Examples using lists:
>
> assert ["c", "a", "b", "c", "c"].index("c", 1) == 3
>
> try:
>     ["a", "b"].index("c")
> except ValueError:
>     print "'c' was not in the list"
> else:
>     raise RuntimeError, "Should never get here"
>
> x = ["a", "b", "c"]
> x.remove("b")
> assert len(x) == 2 and x[0] == "a" and x[1] == "c"
I want a example, maybe: use the a negative index is passed as the
second or third parameter, and see the length changed.
>
> > Is the document wrong?
>
> No. What made you think so?
Sorry for my poor english. do you understand me now? thanks!
>
> Cheers,
> Chris
> --http://blog.rebertia.com

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