Skip to content

Instantly share code, notes, and snippets.

Last active June 22, 2022 15:20
Show Gist options
  • Save ChrisBeaumont/5758381 to your computer and use it in GitHub Desktop.
Save ChrisBeaumont/5758381 to your computer and use it in GitHub Desktop.
Demystifying Python Descriptors
@font-face {
font-family: "Computer Modern";
src: url('');
margin-left:16% !important;
h1 {
font-family: Helvetica, serif;
margin-bottom: 3px;
font-family: Computer Modern, "Helvetica Neue", Arial, Helvetica, Geneva, sans-serif;
line-height: 145%;
font-size: 130%;
div.text_cell_render li {
line-height: 145%;
div.text_cell_render code {
color: rgb(40, 114, 43);
font-family: "Source Code Pro", source-code-pro,Consolas, monospace;
font-size: 80%;
font-family: "Source Code Pro", source-code-pro,Consolas, monospace;
display: None;
.text_cell_render h5 {
font-weight: 300;
font-size: 16pt;
color: #4057A1;
font-style: italic;
margin-bottom: .5em;
margin-top: 0.5em;
display: block;
color: rgb( 240, 20, 20 )
TeX: {
extensions: ["AMSmath.js"]
tex2jax: {
inlineMath: [ ['$','$'], ["\\(","\\)"] ],
displayMath: [ ['$$','$$'], ["\\[","\\]"] ]
displayAlign: 'center', // Change this to 'center' to center equations.
styles: {'.MathJax_Display': {"margin": 4}}
Display the source blob
Display the rendered blob
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Copy link

Good point -- I've made the fix. Thanks!

Copy link

First of all, thanks for the great article, it's informative and easy to follow! I wanted to let you know the link posted under the "UPDATE" is actually dead at this point.

Also, I can't immediately see a "natural" way to combine multiple descriptors to enforce multiple constraints (say negative and even).. am I missing something? I'm asking purely out of curiosity.

Copy link

The correct link seem to be this. If that doesn't work either, just go to the archives for January 2012 here and search for "descriptor".

And yes, it is indeed a great read, thanks for putting this together!

Copy link

@ChrisBeaumont i observed a typo in the following code fragment. "-1" value needs to be changed to "-100"

m = Movie('Casablanca', 97, 102, 964000, 1300000) print m.budget # calls Movie.budget.__get__(m, Movie) m.rating = 100 # calls Movie.budget.__set__(m, 100) try: m.rating = -1 # calls Movie.budget.__set__(m, -100) except ValueError: print "Woops, negative value"

Copy link

@ChrisBeaumont, @jaimefrio The link seems to be wrong again. ;-). I think the correct link is:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment