Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Demystifying Python Descriptors
<style>
@font-face {
font-family: "Computer Modern";
src: url('http://mirrors.ctan.org/fonts/cm-unicode/fonts/otf/cmunss.otf');
}
div.cell{
width:800px;
margin-left:16% !important;
margin-right:auto;
}
h1 {
font-family: Helvetica, serif;
}
h4{
margin-top:12px;
margin-bottom: 3px;
}
div.text_cell_render{
font-family: Computer Modern, "Helvetica Neue", Arial, Helvetica, Geneva, sans-serif;
line-height: 145%;
font-size: 130%;
width:800px;
margin-left:auto;
margin-right:auto;
}
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%;
}
.CodeMirror{
font-family: "Source Code Pro", source-code-pro,Consolas, monospace;
}
.prompt{
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;
}
.warning{
color: rgb( 240, 20, 20 )
}
</style>
<script>
MathJax.Hub.Config({
TeX: {
extensions: ["AMSmath.js"]
},
tex2jax: {
inlineMath: [ ['$','$'], ["\\(","\\)"] ],
displayMath: [ ['$$','$$'], ["\\[","\\]"] ]
},
displayAlign: 'center', // Change this to 'center' to center equations.
"HTML-CSS": {
styles: {'.MathJax_Display': {"margin": 4}}
}
});
</script>
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@ChrisBeaumont
Copy link
Author

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

@szepi1991
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.

@jaimefrio
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!

@amarlakhtakia
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"

@gdementen
Copy link

@ChrisBeaumont, @jaimefrio The link seems to be wrong again. ;-). I think the correct link is: https://mail.python.org/pipermail/python-list/2012-January/618572.html

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