Re: python3 byte decode

2017-11-07 Thread Chris Angelico
On Wed, Nov 8, 2017 at 4:36 PM, Ali Rıza KELEŞ  wrote:
>> To be more clear here, usually when humans say "identical" they mean having
>> exactly the same value or attributes.
>> Here, Chris means that the two strings are actually the same object rather
>> than two equivalent objects. "is" tests the former (the same object). "=="
>> is for testing the latter (the objects have the same value).
>
> Initially the 'is' compared returned value with None, I changed the
> code and it remained as is. After i have noticed this issue.
>
> Using `is` to compare with None  is OK, isn't it?

Yes, that's correct. None is a singleton, so you check for it by
identity. There are other uses of identity checks, such as:

if str is bytes:
# we're running Python 2, so encode/decode appropriately

if blah() is NotImplemented:
# use a fallback

But when you're working with strings or numbers, you'll generally want
to use equality checks.

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


Re: python3 byte decode

2017-11-07 Thread Ali Rıza KELEŞ
Hi,

On 5 November 2017 at 04:06, Cameron Simpson  wrote:
> On 04Nov2017 01:47, Chris Angelico  wrote:
>>
>> On Fri, Nov 3, 2017 at 8:24 PM, Ali Rıza KELEŞ 
>> wrote:
>>>
>>> Yesterday, while working with redis, i encountered a strange case.
>>>
>>> I want to ask why is the following `True`
>>>
>>> ```
>>> "s" is b"s".decode()
>>> ```
>>>
>>> while the followings are `False`?
>>>
>>> ```
>>> "so" is b"so".decode()
>>> "som" is b"som".decode()
>>> "some" is b"some".decode()
>>> ```
>>>
>>> Or vice versa?
>>>
>>> I read that `is` compares same objects, not values. So my question is
>>> why "s" and b"s".decode() are same objects, while the others aren't?
>>>
>>> My python version is 3.6.3.


> For speed and memory reasons, Python notices small values of strings and
> ints, and allocates them only once. So When your write:
>
>  a = "s"
>  b = "s"
>
> Python will reuse the same str object for both. Because of this, not only is
> "a == b" (i.e. they have the same value) but also "a is b" (a and b refer to
> the same object). But this is not guarrenteed, and certainly for larger
> values Python doesn't bother. Eg:

Actually I guessed that it should be a caching mechanism or something
like, but i was not sure since I do not know python internals in
detail.


>> You shouldn't be comparing string objects with 'is'. Sometimes two
>> equal strings will be identical, and sometimes they won't. All you're
>> seeing is that the interpreter happened to notice and/or cache this
>> particular lookup.
>
>
> To be more clear here, usually when humans say "identical" they mean having
> exactly the same value or attributes.
> Here, Chris means that the two strings are actually the same object rather
> than two equivalent objects. "is" tests the former (the same object). "=="
> is for testing the latter (the objects have the same value).

Initially the 'is' compared returned value with None, I changed the
code and it remained as is. After i have noticed this issue.

Using `is` to compare with None  is OK, isn't it?

Cameron, Terry, Chris thanks for your replies in depth.

-
Ali Riza

>>
>>

>
>
>  a = "ghghghghghg"
>  b = "ghghghghghg"
>
> Here they will have the same value but be different objects. So "==" will
> still return True, but "is" would return False.
>
> You should usually be using "==" to compare things. "is" has its place, but
> it is usually not what you're after.
>
> In your example code, b"s".decode() returns the string value "s", and Python
> is internally deciding to reuse the existing "s" from the left half of your
> comparison. It can do this because strings are immutable. (For example, "+="
> on a string makes a new string).
>
> Hoping this is now more clear,
> Cameron Simpson  (formerly c...@zip.com.au)
> --
> https://mail.python.org/mailman/listinfo/python-list



-- 
--
Ali Rıza Keleş
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: python3 byte decode

2017-11-04 Thread Cameron Simpson

On 04Nov2017 01:47, Chris Angelico  wrote:

On Fri, Nov 3, 2017 at 8:24 PM, Ali Rıza KELEŞ  wrote:

Yesterday, while working with redis, i encountered a strange case.

I want to ask why is the following `True`

```
"s" is b"s".decode()
```

while the followings are `False`?

```
"so" is b"so".decode()
"som" is b"som".decode()
"some" is b"some".decode()
```

Or vice versa?

I read that `is` compares same objects, not values. So my question is
why "s" and b"s".decode() are same objects, while the others aren't?

My python version is 3.6.3.


You shouldn't be comparing string objects with 'is'. Sometimes two
equal strings will be identical, and sometimes they won't. All you're
seeing is that the interpreter happened to notice and/or cache this
particular lookup.


To be more clear here, usually when humans say "identical" they mean having 
exactly the same value or attributes. 

Here, Chris means that the two strings are actually the same object rather than 
two equivalent objects. "is" tests the former (the same object). "==" is for 
testing the latter (the objects have the same value).


For speed and memory reasons, Python notices small values of strings and ints, 
and allocates them only once. So When your write:


 a = "s"
 b = "s"

Python will reuse the same str object for both. Because of this, not only is "a 
== b" (i.e. they have the same value) but also "a is b" (a and b refer to the 
same object). But this is not guarrenteed, and certainly for larger values 
Python doesn't bother. Eg:


 a = "ghghghghghg"
 b = "ghghghghghg"

Here they will have the same value but be different objects. So "==" will still 
return True, but "is" would return False.


You should usually be using "==" to compare things. "is" has its place, but it 
is usually not what you're after.


In your example code, b"s".decode() returns the string value "s", and Python is 
internally deciding to reuse the existing "s" from the left half of your 
comparison. It can do this because strings are immutable. (For example, "+=" on 
a string makes a new string).


Hoping this is now more clear,
Cameron Simpson  (formerly c...@zip.com.au)
--
https://mail.python.org/mailman/listinfo/python-list


Re: python3 byte decode

2017-11-03 Thread Terry Reedy

On 11/3/2017 5:24 AM, Ali Rıza KELEŞ wrote:

Hi,

Yesterday, while working with redis, i encountered a strange case.

I want to ask why is the following `True`

```
"s" is b"s".decode()
```

while the followings are `False`?

```
"so" is b"so".decode()
"som" is b"som".decode()
"some" is b"some".decode()
```

Or vice versa?

I read that `is` compares same objects, not values. So my question is
why "s" and b"s".decode() are same objects, while the others aren't?


For the same reason as

>>> a = 1
>>> b = 1
>>> a is b
True
>>> a = 1000
>>> b = 1000
>>> a is b
False

For CPython, 'small' ints are cached on startup.  Ditto for 'small' 
strings, which I think includes all 128 ascii chars, and maybe latin1 
chars.  Details depends on the implemention and version.  The main use 
of 'is' for immutable builtins is for developers to test the 
implementation in the test suite.


--
Terry Jan Reedy


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


Re: python3 byte decode

2017-11-03 Thread Chris Angelico
On Fri, Nov 3, 2017 at 8:24 PM, Ali Rıza KELEŞ  wrote:
> Hi,
>
> Yesterday, while working with redis, i encountered a strange case.
>
> I want to ask why is the following `True`
>
> ```
> "s" is b"s".decode()
> ```
>
> while the followings are `False`?
>
> ```
> "so" is b"so".decode()
> "som" is b"som".decode()
> "some" is b"some".decode()
> ```
>
> Or vice versa?
>
> I read that `is` compares same objects, not values. So my question is
> why "s" and b"s".decode() are same objects, while the others aren't?
>
> My python version is 3.6.3.

You shouldn't be comparing string objects with 'is'. Sometimes two
equal strings will be identical, and sometimes they won't. All you're
seeing is that the interpreter happened to notice and/or cache this
particular lookup.

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


python3 byte decode

2017-11-03 Thread Ali Rıza KELEŞ
Hi,

Yesterday, while working with redis, i encountered a strange case.

I want to ask why is the following `True`

```
"s" is b"s".decode()
```

while the followings are `False`?

```
"so" is b"so".decode()
"som" is b"som".decode()
"some" is b"some".decode()
```

Or vice versa?

I read that `is` compares same objects, not values. So my question is
why "s" and b"s".decode() are same objects, while the others aren't?

My python version is 3.6.3.

Thanks.

-- 
--
Ali Rıza Keleş
-- 
https://mail.python.org/mailman/listinfo/python-list