[Tutor] Test Question

2013-07-01 Thread John Steedman
Good morning all,

A question that I am unsure about.  I THINK I have the basics, but I am not
sure and remain curious.

1. What does this mean?
>>> if my_object in my_sequence:
...

2. What can go wrong with this? What should a code review pick up on?

I believe that "my_sequence" might be a either container class or a
sequence type. An effective __hash__ function would be required for each
"my_object".  I HTINK you'd need to avoid using floating point variables
that might round incorrectly.

Are there other issues?

Many thanks indeed.
John
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Test Question

2013-07-01 Thread Dave Angel

On 07/01/2013 05:58 AM, John Steedman wrote:

Good morning all,

A question that I am unsure about.  I THINK I have the basics, but I am not
sure and remain curious.

1. What does this mean?

if my_object in my_sequence:

...


We can be sure what 'if' and 'in' mean, but not the other two items. 
They're just names, and the behavior of the test will depend on the 
types of the objects the names are bound to.  By calling it my_sequence, 
you're implying that the object is not only a collection, but an ordered 
one.  So if we trust the names, this will iterate through the sequence, 
testing each item in the sequence against my_object for "==" and stop 
when a match is found.  If one is found the if clause will execute, and 
if the sequence is exhausted without finding one, the else clause (or 
equivalent) will execute.





2. What can go wrong with this? What should a code review pick up on?


The main thing that can go wrong is that the objects might not match the 
names used.  For example, if the name my_sequence is bound to an int, 
you'll get a runtime exception.


Second, if the items look similar (eg. floating point, but not limited 
to that), but aren't actually equal, you could get a surprise.  For 
example if my_object is a byte string, and one of the items in 
my_sequence is a unicode string representing exactly the same thing. 
Python 2 will frequently consider them the same, and Python 3 will know 
they're different.


Third if my_object is something that doesn't equal anything else, such 
as a floating point NAN.  Two NANs are not equal, and a NAN is not even 
equal to itself.


By a different definition of 'wrong' if the sequence is quite large, and 
if all its items are hashable and it may have been faster to pass a dict 
instead of a sequence.  And if my_sequence is a dict, it'll probably be 
faster, but the name is a misleading one.




I believe that "my_sequence" might be a either container class or a
sequence type. An effective __hash__ function would be required for each
"my_object".


"in" doesn't care if there's a __hash__ function.  It just cares if the 
collection has a __contains__() method.  If the collection is a dict, 
the dict will enforce whatever constraints it needs.  If the collection 
is a list, no has is needed, but the __contains__() method will probably 
be slower.  In an arbitrary sequence it won't have a __contains__() 
method, and I believe 'in' will iterate.



I HTINK you'd need to avoid using floating point variables
that might round incorrectly.



One of the issues already covered.


Are there other issues?



Those are all I can think of off the top of my head.


--
DaveA
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Test Question

2013-07-01 Thread Hugo Arts
On Mon, Jul 1, 2013 at 1:01 PM, Dave Angel  wrote:

> On 07/01/2013 05:58 AM, John Steedman wrote:
>
>>
>> I believe that "my_sequence" might be a either container class or a
>> sequence type. An effective __hash__ function would be required for each
>> "my_object".
>>
>
> "in" doesn't care if there's a __hash__ function.  It just cares if the
> collection has a __contains__() method.  If the collection is a dict, the
> dict will enforce whatever constraints it needs.  If the collection is a
> list, no has is needed, but the __contains__() method will probably be
> slower.  In an arbitrary sequence it won't have a __contains__() method,
> and I believe 'in' will iterate.
>
>
It will indeed iterate. It's always useful to check the language reference
in cases like these to see precise behaviour:
http://docs.python.org/2/reference/expressions.html#membership-test-details

Hugo
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Test Question

2013-07-01 Thread Steven D'Aprano

On 01/07/13 22:15, Oscar Benjamin wrote:

On 1 July 2013 12:01, Dave Angel  wrote:

Third if my_object is something that doesn't equal anything else, such as a
floating point NAN.  Two NANs are not equal, and a NAN is not even equal to
itself.


Many builtin collection types do an identity 'is' check before an
equality '==' check so nan behaviour depends on whether it is the
exact same nan:


float('nan') in [1, 2, 3, float('nan')]

False

nan = float('nan')
nan in [1, 2, 3, nan]

True

I don't know to what extent that is a deliberate feature or an
optimisation that wasn't fully considered but it at least maintains
the following invariant:



It's an optimization, but one which by declaration of the BDFL stands. It 
simply isn't worth slowing down list processing for something as uncommon as 
NANs. Lists are general purpose containers, and shouldn't be expected to 
understand every little quirk of every weird value.

I am a strong supporter of the IEEE 754 behaviour of NANs, namely that if x is 
a NAN, x == x returns False. There are good reasons for this, *in the context 
of numerical computing*. Nevertheless, I support Guido's ruling on this. Lists 
are not part of IEEE 754, neither is object identity, and if somebody wants a 
list that is NAN-aware, they can make one:

# Untested.
class MyList(list):
def __contains__(self, value):
if math.isnan(value): return False
return super().__contains__(value)



--
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Test Question

2013-07-01 Thread Steven D'Aprano

On 01/07/13 19:58, John Steedman wrote:

Good morning all,

A question that I am unsure about.  I THINK I have the basics, but I am not
sure and remain curious.

1. What does this mean?

if my_object in my_sequence:

...



Others have already answered this, but for completion, it is testing whether "my_object" 
can be found as an element of the sequence, iterable, or container "my_sequence".



2. What can go wrong with this? What should a code review pick up on?


Depends on context. Without context, nearly anything could go wrong:

NameError -- perhaps one or both of the names are undefined;

TypeError -- perhaps the names are misleading, and my_sequence is not a 
sequence at all;

Perhaps my_sequence is an infinite iterator and the above will never complete;

etc.


I believe that "my_sequence" might be a either container class or a
sequence type. An effective __hash__ function would be required for each
"my_object".


Correct, if my_sequence is in fact a dict or other mapping that relies on 
hashing.

But in fact it's not just the presence of a __hash__ method on my_object which 
is required, but that the __hash__ method can actually return. E.g. tuples have 
a __hash__ method, but that relies on every element of the tuple being hashable:

py> (1, 2, 3) in {}
False
py> (1, 2, [3]) in {}
Traceback (most recent call last):
  File "", line 1, in 
TypeError: unhashable type: 'list'



I HTINK you'd need to avoid using floating point variables
that might round incorrectly.


No, Python floats are not rounded when doing containment testing. They may have 
been rounded earlier, but `x in container` will use the full precision of the 
float.



Are there other issues?


Nothing obvious to me.




--
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to remove from mailing list when server access blocked?

2013-07-01 Thread Dave Angel

On 07/01/2013 12:26 PM, jhame...@medford.k12.ma.us wrote:

Hello,

Can anyone tell me how to remove myself from this mailing list? When I
click on unsubscribe my browser displays a message that I can not access
the unsubscribe url or server because my IP is on a blacklist at
Spamhaus. I am writing from my work email and don't have permission to
change settings, etc.

What are my options?




Sorry that Ramit didn't read your email carefully enough.

I'd suggest you tell your company's IT person about the Spamhaus 
problem, so she can get it cleared up.


But you can unsubscribe entirely by email:

Send an email to:

tutor-requ...@python.org

with a subject ofHelp

and you'll get an email back with instructions.  Probably all you'll 
need then is to reply, changing the subject to Unsubscribe




--
DaveA
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Test Question

2013-07-01 Thread John Steedman
Many thanks, everyone.  Great answers. I decided to read the manual
properly.  May take some time but well worth it.


On Mon, Jul 1, 2013 at 2:40 PM, Steven D'Aprano  wrote:

> On 01/07/13 19:58, John Steedman wrote:
>
>> Good morning all,
>>
>> A question that I am unsure about.  I THINK I have the basics, but I am
>> not
>> sure and remain curious.
>>
>> 1. What does this mean?
>>
>>> if my_object in my_sequence:
>
 ...
>>
>
>
> Others have already answered this, but for completion, it is testing
> whether "my_object" can be found as an element of the sequence, iterable,
> or container "my_sequence".
>
>
>
>  2. What can go wrong with this? What should a code review pick up on?
>>
>
> Depends on context. Without context, nearly anything could go wrong:
>
> NameError -- perhaps one or both of the names are undefined;
>
> TypeError -- perhaps the names are misleading, and my_sequence is not a
> sequence at all;
>
> Perhaps my_sequence is an infinite iterator and the above will never
> complete;
>
> etc.
>
>
>  I believe that "my_sequence" might be a either container class or a
>> sequence type. An effective __hash__ function would be required for each
>> "my_object".
>>
>
> Correct, if my_sequence is in fact a dict or other mapping that relies on
> hashing.
>
> But in fact it's not just the presence of a __hash__ method on my_object
> which is required, but that the __hash__ method can actually return. E.g.
> tuples have a __hash__ method, but that relies on every element of the
> tuple being hashable:
>
> py> (1, 2, 3) in {}
> False
> py> (1, 2, [3]) in {}
> Traceback (most recent call last):
>   File "", line 1, in 
> TypeError: unhashable type: 'list'
>
>
>
>  I HTINK you'd need to avoid using floating point variables
>> that might round incorrectly.
>>
>
> No, Python floats are not rounded when doing containment testing. They may
> have been rounded earlier, but `x in container` will use the full precision
> of the float.
>
>
>  Are there other issues?
>>
>
> Nothing obvious to me.
>
>
>
>
>
> --
> Steven
> __**_
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Boosts

2013-07-01 Thread Jack Little
In my concept, when the player buys a boost, their karma (a global) is 
multiplied by 25% when added to. How would I do this?

Thanks.___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Boosts

2013-07-01 Thread Dave Angel

On 07/01/2013 04:41 PM, Jack Little wrote:

In my concept, when the player buys a boost, their karma (a global) is 
multiplied by 25% when added to. How would I do this?




Making a changeable global is almost certainly a mistake.

But to multiply by 25%, you simply divide by 4.

karma /= 4



--
DaveA
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Boosts

2013-07-01 Thread Steven D'Aprano

On 02/07/13 06:41, Jack Little wrote:

In my concept, when the player buys a boost, their karma (a global) is 
multiplied by 25% when added to. How would I do this?


karma = karma * 0.25

which can be written as:

karma *= 0.25

If this is inside a function (as it should be!) you will need to indent the 
line, and include a declaration at the top of the function:

global karma
karma *= 0.25


However, are you sure you want to multiply their karma by 25%? That makes it 
smaller. Perhaps you want to INCREASE it by 25%, which would be calculated like 
this:

global karma
karma *= 1.25


or perhaps:

karma = karma + 0.25*karma



--
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor