bslatkin / effectivepython

Effective Python: Second Edition — Source Code and Errata for the Book
https://effectivepython.com
2.24k stars 718 forks source link

Item 31 some codes results are not consistent with the book in python 3.6 #52

Closed wtbsw closed 5 years ago

wtbsw commented 6 years ago

class Grade(object): def init(self): self._value = 0

def __get__(self, instance, instance_type):
    return self._value

def __set__(self, instance, value):
    if not (0 <= value <= 100):
        raise ValueError('Grade must be between 0 and 100')
    self._value = value

first_exam = Exam() first_exam.writing_grade = 82 first_exam.science_grade = 99 print('Writing', first_exam.writing_grade) print('Science', first_exam.science_grade)

results

Writing 82

Science 99

second_exam = Exam() second_exam.writing_grade = 75 print('Second', second_exam.writing_grade, 'is right') print('First', first_exam.writing_grade, 'is wrong')

result

Second 75 is right

First 82 is wrong

The result is right when the code run , compared with book' result where

Second 75 is right

First 75 is wrong

Glad to discuss with you. Thank you.

bslatkin commented 5 years ago

What version of Python are you using? The corresponding example code checked into this repository works as expected in Python 2 and 3 for me. Maybe you can attach a gist that reproduces this? Please reopen if you can. Thanks.