顶部和底部值[PEP:326] [英] Top and Bottom Values [PEP: 326]

查看:58
本文介绍了顶部和底部值[PEP:326]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个PEP被拒绝了。但是我有一个问题,

会因极端值而受益,并且PEP中给出的样本实际上是不可靠的

for my目的。


我编写了自己的模块,其工作方式类似但是

有点扩展。下面是一个如何使用它的例子以及我如何使用它但却被卡住了。

来自极端进口的
顶部

I know this PEP is rejected. However I have a problem that
would benefit from having extreme values and the sample
implementation that is given in the PEP is unsatifactory
for my purpose.

I had written my own module, which works similarly but
is somewhat extended. Here is an example of how it can
be used and how I would like to use it but get stuck.

from extreme import Top


>> Top
>>Top



Top

Top


>> Top + 1
>>Top + 1



Top

Top


>>热门 - 30
>>Top - 30



热门

Top


>> Top 1e99
>>Top 1e99



True

True


>> lst = range(10)
lst [:Top]
>>lst = range(10)
lst[:Top]



回溯(最近一次调用最后一次):

文件"< stdin>",第1行,在?

TypeError:切片索引必须是整数或无


所以这就是我被困的地方。我需要这个顶级值在

的上下文中,它可以用作开始或停止值
在一个切片中。我的想法是,因为在这种情况下,停止值大于lst的长度只会返回lst的副本,使用Top会以这种方式表现如此

也是。但是我没有看到这样做的方法。


那么有人可以提出想法来解决这个问题吗?

如果没有,是否有可能PEP 326复活了?


-

Antoon Pardon

Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: slice indices must be integers or None

So this is where I am stuck. I need this Top value in
a context where it can be used as a start or stop value
in a slice. My idea was that since a stop value greater
than the length of lst in this context would simply return
a copy of lst, using Top in such a way should behave so
too. However I don''t see a way of doing that.

So can someone provide ideas to get this behaviour?
If not is there a chance that PEP 326 gets revived?

--
Antoon Pardon

推荐答案

在消息< sl ******************** @ rcpc42.vub.ac.be>中,Antoon Pardon写道:
In message <sl********************@rcpc42.vub.ac.be>, Antoon Pardon wrote:

我需要在

a上下文中使用此最高值,它可以用作开始或结束值

在一个切片中。
I need this Top value in
a context where it can be used as a start or stop value
in a slice.



但是索引允许的唯一有效值是0到

数组的长度。不允许使用更大的整数,那么为什么要允许Top?


But the only valid values allowed for indices are 0 up to the length of the
array inclusive. Larger integers are not allowed, so why should Top be
allowed?




Lawrence D''Oliveiro写道:

Lawrence D''Oliveiro wrote:

在消息< sl ******************** @ rcpc42.vub.ac.be> ;,Antoon Pardon写道:
In message <sl********************@rcpc42.vub.ac.be>, Antoon Pardon wrote:

我在

a上下文中需要这个最高值,它可以用作开始值或停止值

I need this Top value in
a context where it can be used as a start or stop value
in a slice.



但是索引允许的唯一有效值是0,直到

数组的长度为止。不允许使用更大的整数,那么为什么要允许Top?



But the only valid values allowed for indices are 0 up to the length of the
array inclusive. Larger integers are not allowed, so why should Top be
allowed?



不允许更大的整数作为下标,但切片更多

宽容:


| >> array = ['''',''b'',''c'']

| >>数组[:1]

| [''a'']

| >>数组[:2]

| ['''',''b'']

| >>数组[:3]

| ['''',''b'',''c'']

| >>数组[:4]

| ['''',''b'',''c'']

| >>数组[:987654321]

| ['''',''b'',''c'']


BTW,也允许负面下标;参见例如
http://docs.python.org/ tut / node5.htm ... 00000000000000


HTH,

John

Larger integers are not allowed as subscripts, but slicing is more
tolerant:

| >>array = [''a'', ''b'', ''c'']
| >>array[:1]
| [''a'']
| >>array[:2]
| [''a'', ''b'']
| >>array[:3]
| [''a'', ''b'', ''c'']
| >>array[:4]
| [''a'', ''b'', ''c'']
| >>array[:987654321]
| [''a'', ''b'', ''c'']

BTW, negative subscripts are allowed, too; see e.g.
http://docs.python.org/tut/node5.htm...00000000000000

HTH,
John


Antoon Pardon写道:
Antoon Pardon wrote:

我编写了自己的模块,其工作方式类似但是

有所扩展。下面是一个如何使用它的例子以及我如何使用它但却被卡住了。

来自极端进口的
顶部
I had written my own module, which works similarly but
is somewhat extended. Here is an example of how it can
be used and how I would like to use it but get stuck.

from extreme import Top

>>> Top
>>>Top



Top

Top


>>> Top + 1
>>>Top + 1



Top

Top


>>> Top - 30
>>>Top - 30



Top

Top


>>> Top 1e99
>>>Top 1e99



True

True


>>> lst = range(10)
lst [:顶部]
>>>lst = range(10)
lst[:Top]



回溯(最近一次调用最后一次):

文件"< stdin>",第1行,在?

TypeError:切片索引必须是整数或无


所以这就是我被困的地方。我需要这个顶级值在

的上下文中,它可以用作开始或停止值
在一个切片中。我的想法是,因为在这种情况下,停止值大于lst的长度只会返回lst的副本,使用Top会以这种方式表现如此

也是。但是,我没有看到这样做的方法。


那么有人可以提供想法来获得这种行为吗?

Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: slice indices must be integers or None

So this is where I am stuck. I need this Top value in
a context where it can be used as a start or stop value
in a slice. My idea was that since a stop value greater
than the length of lst in this context would simply return
a copy of lst, using Top in such a way should behave so
too. However I don''t see a way of doing that.

So can someone provide ideas to get this behaviour?


>> import sys
class Top(object ):
>>import sys
class Top(object):



.... def __index __(self):

.... return sys .maxint

....

.... def __index__(self):
.... return sys.maxint
....


>> Top = Top()
范围(5)[:Top]
>>Top = Top()
range(5)[:Top]



[0,1,2,3, 4]


这当然可以通过许多有趣的方式失败...


彼得

[0, 1, 2, 3, 4]

This can of course fail in many interesting ways...

Peter

这篇关于顶部和底部值[PEP:326]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆