Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Python3 support #23

Merged
merged 1 commit into from
May 15, 2020
Merged

Conversation

Recursing
Copy link
Contributor

@Recursing Recursing commented May 15, 2020

Edits:

  • Replace print statement with print function
  • Replace xrange with range
  • Replace iter* in dict methods (e.g. iteritems())
  • Replace generator.next() with next(generator) (available since python 2.6 release in 2008)
  • Explicit id based hash function for Pmf (see TypeError: unhashable type: 'Pmf' (thinkbayes.py) #19 )

All changes have been tested both on python3 and on python2, in python2 using range instead of xrange (items() instead of iteritems(), and so on) doesn't seem to cause significant increased memory usage or performance regressions

Edits:
 - Replace print statement with print function
 - Replace xrange with range
 - Replace iter* in dict methods (e.g. iteritems())
 - Replace generator.next() with next(generator) (available since python 2.6 release in 2008)
 - Explicit id based hash function for Pmf
@Recursing
Copy link
Contributor Author

Recursing commented May 15, 2020

The id based hash of Pmf (and the __eq__ overloaded to be probabilistic) can give problems when used with normal python dictionaries or built-ins, that assume the standard behavior from those function, see https://docs.python.org/3.8/reference/datamodel.html#object.__hash__

If a class defines mutable objects and implements an __eq__() method, it should not implement __hash__(), since the implementation of hashable collections requires that a key’s hash value is immutable (if the object’s hash value changes, it will be in the wrong hash bucket).

@AllenDowney AllenDowney merged commit 77eed00 into AllenDowney:master May 15, 2020
@AllenDowney
Copy link
Owner

Thank you!

@Recursing
Copy link
Contributor Author

Thank you for this awesome book! Can't wait to read the second version!

@SvenAG
Copy link

SvenAG commented Jun 8, 2020

Thank you, this also fixes the issue with the mixtures of dice in chapter 5!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants