Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #196965

Re: Two python issues

From dn <PythonList@DancesWithMice.info>
Newsgroups comp.lang.python
Subject Re: Two python issues
Date 2024-11-06 10:41 +1300
Organization DWM
Message-ID <mailman.85.1730843499.4695.python-list@python.org> (permalink)
References <700403c2-e052-4670-b2ec-eaf9b4babada@pandora.be> <CANy1k1iML9=cKxhQfASBZDwqc_4N_E66MgXhddPf6zna3G-ckw@mail.gmail.com> <8faeaf6f-0110-41bd-9e21-1fc4d57b42ae@DancesWithMice.info>

Show all headers | View raw


On 6/11/24 10:08, Jason Friedman via Python-list wrote:
>>
>> (a) An error-prone "feature" is returning -1 if a substring is not found
>> by "find", since -1 currently refers to the last item. An example:
>>
>>   >>> s = 'qwertyuiop'
>>   >>> s[s.find('r')]
>> 'r'
>>   >>> s[s.find('p')]
>> 'p'
>>   >>> s[s.find('a')]
>> 'p'
>>   >>>
>>
>> If "find" is unsuccessful, an error message is the only clean option.
>> Moreover, using index -1 for the last item is a bad choice: it should be
>> len(s) - 1 (no laziness!).
>>
> 
> I'm not sure if this answers your objection but the note in the
> documentation (https://docs.python.org/3/library/stdtypes.html#str.find)
> says:
> 
> The find() method should be used only if you need to know the position of
> sub.
> 
> I think the use case above is a little bit different.


Not really, there are two questions:

1. is x in sequence (or in this case "not in")
2. where is x within sequence (find())

There are situations where one might be used, similarly where the other 
will be used, and still more where both apply.

That said, and with @Cameron's observation, the idea that a function's 
return-value (appears to) performs two functionalities is regarded as a 
'code-smell' in today's world - either it indicates "found" or it 
indicates "where found" (see also various APIs which return both a 
boolean: success/fail, and a value: None/valid-info).

The problem with the third scenario being that purity suggests we should 
use both (1) and (2) which seems like duplication - and is certainly 
going to take more CPU time.
(will such be noticeable in your use-case?)

Backward-compatibility... ('nuff said!)


With reference to the OP content about slicing:
- Python's memory-addressing is different from many other languages. 
Thus, requires study before comparison/criticism
- there are major differences in what can be accomplished with mutable 
and immutable objects

-- 
Regards,
=dn

Back to comp.lang.python | Previous | Next | Find similar


Thread

Re: Two python issues dn <PythonList@DancesWithMice.info> - 2024-11-06 10:41 +1300

csiph-web