Discovering Descriptors

Video in TIB AV-Portal: Discovering Descriptors

Formal Metadata

Discovering Descriptors
Title of Series
CC Attribution - NonCommercial - ShareAlike 3.0 Unported:
You are free to use, adapt and copy, distribute and transmit the work or content in adapted or unchanged form for any legal and non-commercial purpose as long as the work is attributed to the author in the manner specified by the author or licensor and the work or content is shared also in adapted form only under the conditions of this license.
Release Date

Content Metadata

Subject Area
Discovering Descriptors [EuroPython 2017 - Talk - 2017-07-11 - PythonAnywhere Room] [Rimini, Italy] Python is full of amazing (yet often overlooked) features, that can help us write better, cleaner, and more maintainable code. One of them is the descriptors interface. By means of descriptors, we can run code when accessing or setting properties of another object. This can have a lot of applications, and keeping descriptors in mind might come in handy when facing a hard problem. The goal of the talk is to explore descriptors, and how they can be written, as well as analysing some interesting examples in well-known libraries or projects in order to identify scenarios where descriptors are the Pythonic way to go
Programming language Computer configuration Uniqueness quantification Orientation (vector space) Software Expert system Video game Object (grammar) Mereology Formal language Form (programming)
Game controller Computer configuration Code Control flow Social class
Operations research Presentation of a group Physicalism Core dump Attribute grammar Instance (computer science) Control flow Formal language Personal digital assistant Operator (mathematics) Core dump Energy level Object (grammar) Family
Type theory Type theory Different (Kate Ryan album) Core dump Attribute grammar Staff (military) Instance (computer science) Sweep line algorithm Implementation Mereology
Implementation Type theory State of matter Different (Kate Ryan album) Term (mathematics) Attribute grammar Instance (computer science) Object (grammar) Implementation Data dictionary
Category of being Euler angles Multiplication sign File format Attribute grammar Social class Sweep line algorithm Object (grammar) Summierbarkeit Social class
Particle system File format Multiplication sign String (computer science) File format Representation (politics) Instance (computer science) Parameter (computer programming) Object (grammar) Cartesian coordinate system Event horizon Social class
Virtual reality Matching (graph theory) Uniqueness quantification 40 (number) Social class Object (grammar) Statistics Physical system Social class Physical system
Virtual reality Computer file Euler angles Computer configuration String (computer science) Multiplication sign Instance (computer science) Object (grammar) Statistics Physical system
Key (cryptography) Image resolution Multiplication sign Parameter (computer programming) Data dictionary Mereology Order (biology) String (computer science) Order (biology) Representation (politics) Statement (computer science) Object (grammar) Social class
Category of being Mapping Key (cryptography) Computer file State of matter Instance (computer science) Object (grammar) Statistics Social class
Game controller Personal digital assistant Surface Computer file Boundary value problem Statement (computer science) Object (grammar) Parameter (computer programming) Statistics Family Resultant
Scripting language Observational study Multiplication sign Computer file Instance (computer science) Parameter (computer programming) Disk read-and-write head Cartesian coordinate system Statistics Personal digital assistant Computer configuration Logic Social class Pattern language Object (grammar) Social class
Category of being Computer configuration Euler angles Computer file Social class Instance (computer science) Instance (computer science) Statistics Resultant
Scripting language Revision control Category of being Virtual reality Logic String (computer science) Virtual machine Statistics Physical system
Meta element Implementation Mapping Computer configuration Euler angles Multiplication sign Social class Attribute grammar Configuration space Parameter (computer programming) Social class
Partition (number theory) Category of being Sign (mathematics) Multiplication sign Physical law Attribute grammar Object (grammar) Perspective (visual)
Category of being Multiplication Group action Strategy game Multiplication sign Website Set (mathematics) Attribute grammar Instance (computer science) Counting
Category of being Personal digital assistant Multiplication sign Video game Counting Attribute grammar Instance (computer science) Line (geometry) Counting Mereology
Category of being Mathematics Personal digital assistant Multiplication sign Order (biology) Instance (computer science) Object (grammar)
Category of being Game controller Logic Computer configuration Representation (politics) Instance (computer science) Tracing (software) Perspective (visual) Resultant Smith chart Social class
Scripting language Point (geometry) Default (computer science) Game controller Military base Surface View (database) Decision theory Set (mathematics) Attribute grammar Instance (computer science) Parameter (computer programming) Performance appraisal Object (grammar) Social class
Predictability Euler angles Instance (computer science) Exception handling
Scripting language Euler angles Set (mathematics) Smith chart Exception handling Social class
Scripting language Object (grammar)
Category of being Consistency Function (mathematics) Cellular automaton Similarity (geometry) Mereology Statute Category of being Formal language Formal language
Category of being Function (mathematics) Electronic mailing list Sweep line algorithm Object (grammar) Category of being Formal language Entire function Social class
Functional (mathematics) Hypermedia Function (mathematics) Instance (computer science) Parameter (computer programming) Object (grammar) Category of being Formal language Social class
Functional (mathematics) Type theory Computer configuration Function (mathematics) Social class Instance (computer science) Object (grammar) Mereology Data dictionary Social class
Functional (mathematics) Type theory Information Function (mathematics) Video game Social class Right angle Object (grammar) Social class
Scripting language Functional (mathematics) System call Multiplication sign Keyboard shortcut Constructor (object-oriented programming) Social class Instance (computer science) Object (grammar) Parameter (computer programming) Social class
Game controller Personal digital assistant Protein Electronic signature
Functional (mathematics) Function (mathematics) Order (biology) Code Electronic signature System call Resolvent formalism Electronic signature
Domain name Context awareness Functional (mathematics) Mathematics Root Maize View (database) Software framework Information Process (computing) Cycle (graph theory) Electronic signature
Context awareness Functional (mathematics) Root Logic Multiplication sign Right angle Information Object (grammar) Parameter (computer programming) Social class
Context awareness Shift operator Dependent and independent variables State of matter Parameter (computer programming) Online help Parameter (computer programming) Frame problem Root Error message Process (computing) Information Data type
Scripting language Context awareness Functional (mathematics) Mathematics Root Multiplication sign Equaliser (mathematics) Parameter (computer programming) Information Instance (computer science) Line (geometry) System call
Functional (mathematics) Computer configuration Similarity (geometry) Video game Object (grammar)
Point (geometry) Scripting language Implementation Run time (program lifecycle phase) Matching (graph theory) Shared memory Maxima and minima Entire function Category of being Analogy Interface (computing) Software framework Implementation Library (computing)
Different (Kate Ryan album)
Category of being Gastropod shell Set (mathematics) Pattern language
Revision control Latent heat Implementation Code
the only thing you bring here on this evolved this unique feature of life and and 1st then we start by saying I'm not the
only 1 who's very fond of escudos still finite
many I expressly members in Python will also tell you had with descriptors are the reason being that not only there a powerful feature but they're a fundamental part of the language itself so descriptors you'll go more proficient Python because it's an important tool to hurry a said Ireland understand the language with the answers so that you'll be gone on for expert but I would've 6 something that applies to all mainstream programming languages that have support for option orientation of objects in the form of the senses of
some class I mean there will hold the dart based on an ideas so were we we said uh bodies to them and when we try to request on value we obtain with a well we expect which is pretty much what we said previously always little you would you run something ladies when Europe was a while you often out you will go actually run a rhetorical control by us and the going on is going to be managed by another option of belief from class but basically
nothing really high descriptors descriptors are
a powerful feature of Python by which we can control what happens at the very low level over the basic core operations of the language means that we can control happens when we try to get it said or the other use on you know objects that hobbies and brittle of descriptors so this could there's RT by these opera dual which by phonemes I until it is much
methods as always in Python are especially here is that you made by matching methods in this case these 4 methods Our complying with edited the world brother will basically and the yet said undelete and the last 1 was citing an by from the looks 6 which was an interesting hasn't to language so the rest of the presentation will follow Python physics sees as a reference because there has been
some improvement so that always not only the scoring descriptors are perhaps also rescoring history there so even if you are familiar with them is also interesting to see other new staff are in the core measured by the but it's
going parts 1st specified descriptors are 2 types we see 1 how do know is that there's something subtle differences between them we're going classified I mean they could also known the
descriptors In terms of implementation the difference is whether or not they implement another state if the women the said there also called overriding descriptors and will take precedence always uh instead of a dictionary of the object whereas the non the descriptors we can actually lower right and because the dictionary of culture will take precedence and will be around 1st so let's start with something
simple we can only descriptor but for these
abroad the hobbies that I have an object of class so much use I want automatically ransom competitions and of attitudes on kind of like a property but it does so there are many of you so if I want to a user property 100 Goldman times so users critters and this means that I will have to create at least 2 classes 1 4 this descriptor itself and the other 1 which is a so-called managed class which is a gas that I originally intended to have in the 1st place when I was going to be managed by a descriptor objects so 1st let's inspect
descriptor I'm going to read an object ladies
which I'm going to go they form the on what's
going to the least is going to format at a time into a string so this has a name what will see that white is given you name actually but important a thing here is that the color because of the myriad method because this is what defines this class 2 we ask you to because implements 1 of the methods of this could the particle and the event that takes 2 parameters these stars which is the object being axis at that time and owner which is a reference to the class away do not mentally so we'll try to get the latitude we coding for that object I will return a string representation of that uh daytime so now we can
take a look at the match class held how this is used for the origin because they intended to hop on
forties my logical had these which represent a 5 which raising the most I have fly like object in a unique sound like system and the subsidies if defined by a filename I'm I'm interested in truck you know so the date it was created updated every move
on these 3 attitudes are going to be instances of later but I have similar attitudes which are going to be called aircraft use which are basically the same but with a graphic Str Str created out on their to ease says he's of descriptor this is important it is an important needle for this to the prodigal toward we have to be defined the gasses so they had to because of the use although or otherwise is there will not work so is it real she's
like a grid of a simple maybe claymation by which is CRP that actually managing yet you name created out so that's see now have to things that are individually that cities actually work called to him it so is it like to read me my object my files that we'll find and I'm going to pass a date for the creation of innovation and once again these are instances of a time from the summary I now I request str created out for that option
I had assumed representation of trade that out so in order to see how these happen
it's important to understand in which all and Python resolve things so it's all apart
1st I migrated object I pass all the parameters to the Ecolizer on an cities argued which is the opposite so the 1st thing you will happen is by the will of the standard dictionary of the object I so I do try to fetch it as a key with the name str created that part as it happens is not there because the ocean cost created or not is generally the soul of search fails but is not a problem that is going to continue on the next search is going to be in the dictionary of the class and this time
the keys there because remember that in the March class I defined is carried out to be an instance of the dictionary so the gas has its CRP out and is a mapping a key is not been a key we do I do so you think it might mean OK so and to return the money for the object of the happens we all properties well actually no because this
is I skipped or so but this is my enough to tell the these obstacles and again
method so is going to happen is that instead going to gold boundary yet metal that object and that's the result i on obtaining so in the end
what happened is that when I created the Lakers yard created out by the would go the gas not the descriptor which in this case is says argued out not there yet they will pass the object surface parameter and the cost of a 2nd brother and he said they felt they were in full control of what we're doing so we
can all run a leadership that we want in this case the logic for formatting the delay time according to our requirements there's an our case where when you called on the head when you try to delete something that is a script or but Euclid from the class no from an option in that
case he's does which is the 1st parameter from the it is going to be none and the most common pattern or even to the having the situation is to show returns self which is a script that so is it really a study if you follow keep their eye your axis you for an object you're done the competition that thought was the finding of the yet whereas if you tried it away from the class of an
entire descriptive option with the much like a property like will you define a property enough significant from an instance of the result of the computation be degraded by the property but you called the property from the customer the property option this is as similar
so let's see how this could its name because so far we assume this could was to retrieve the attitude I'm just transforming the way wanted to but actually had explicitly
passed the into the script or I was there need I made skip were able to store the name internally so it's giver somewhat st our abated out I had above the string and they do not to tell this kid into a machine their property of the you under the name of the departure but this is somehow related you and I want to know how these logic I was something like in the less example just read a script or and
to make it able to automatically configure itself so if you wanted to achieve this improved version of Python had to resort to some
complicated stuff for example 1 way of doing so was like having a classic greater undergoes a greater you wanna find a
mapping of each name to lead this script or on the acquired the worsening when you create a descriptor or an hour implementations have seen are weighed metaclasses now again 1 created at the option you you tried to set up creation time might arise and the new but all things and been are way too complicated for something that should be like plain simple so luckily there were some has in Python 3 look 6 and this method was ordered to the class creations which is said name and it receives the class when dollar which is a class suffers from the on the name which is the name of the attitude appears defined in the class a 2nd parameter and is going to recall automatically once the gas is being created so together and each of these would just use a method for automatically storing self the name I'm a descriptor
know the name of a I think that is going to manage in which makes things more simpler ideas from the user perspective because the user doesn't have to know all remember optical fear that is key so
they know how they may see that in the date as could there and we know how this could get the name of the property that the law we might find this moved to something is slightly more complex we got data escaped early signs or owing to implement and the set on the say this is just an example there which to how an object can be any object that has some of all 1 is to automatically count how many times those of users change their
money over time on if I tried to think strategies to achieve these again Python has always offered offers multiple solutions so I can do that with a property for maybe with set the and in this set the can Harlington accountability it's updated all the time by how many properties LB repetitive I could also that we said either you but overriding these matching method or always escape those which I think is much more of simple and comedian as we'll see in the next hour site so 1st give the
1st the name and we know we know now how this group of its name I did on the set method
None of that name so in this case is going to store the a muscle weighs and in the 2nd line is going to restore account name which would mean sees it counts how many times a problem the under the name change their life your time has just for simplicity of for the sake of example in going to assume that in this case it
was in that it starts with count underscore correspond to something that is the count of an hour property and another property itself so now we can move to understand method and C are updated the interesting part
so is it the other said receives the object be modified at that time undervalue the trying to set a lot descriptor in and that time so is it you are going to look furious I'm going to try to request and the property from the object by inspecting the scenario with a name that we know that this could the house it is not there and would decide the counter to seal because it means that never changes while he was the 1st time were something if it is there in their near the were saying that if if the light is different and 1 a cost means there's a change so I'm going to increment the counter where 1 and in any case no matter what the 1 daughter we said they you by there on current the user's currently assigning so in order to exercise these this much and how the
social which is that our about the any can be any object I'm interested in seeing how many different cities and
countries that person has received the so I'm going to find city and country do we both instances of class of use instances of their the skip so it's a Trace Properties amazing what happens here is that 1st I said something to the logic and we still will come the 1st set to 0 then when that tourism or from 1 city to another we see that the counters are incremented unsworn also for you can get the rest of but the important thing is that if you look at these this is from the user perspective this is complete inspired you uses only 1 values or activity noted I was hopin is the highest is a descriptor which is always taking
place and they didn't turn a representation of the option so this is completely transparent to the user idealist full control or what's happening which I just said something the value so again similar the 1st example results in the sure
of who goes when we tried to set something script has than the said we find I was going to happen is that to gold the class that is cleaved or not said I soon to pass on the object surface parameter and you were
trying to assign a physical parameter i in in the middle where full control of the lot we can use these to create evaluators default values are important decision to at this point maybe is becoming clearer you might already guess what and that lead in my view doing basic these the method yes assume I have as it was going to be going try to delete something bases could there has his method define so for example a much and I want to protect
some attitudes against deletion worry
if there or accident but 1 way of doing so would be to create eyes kid that implements than the lead on race exception saying that you can delete these on I for the subject so for somebody my obligation if I want to predict user name and the nation 1 possible way of doing so would be to
actually create out of class attitude I use this these descriptors the the I we see that the if I create USA unattractive deleted you raise an exception setting
me exactly where 1 whereas if I tried to leave anything else it will let me because they're just rare I think it's nothing particularly so that's the from
eating something that has script or something that it doesn't always
so OK now we know the basics all descriptors we have seen the basic methods on roughly held implemented embassy basically hopefully with some examples you can actually take
that advantage but lower making sense out of the script like where actually used in it properly I basically as good there is any
is like any would Python object so Proi would
escape the race 1 that makes sense with Python E cell is consistent with the language so it's quite funny as will call it so now to say
a broader way is to take a look a whole descriptors are use internally wasted Python because as it happens that employed in the language of statute are from them in the part of the language itself announced on beginning of the talk for example properties are descriptors if you recall the 1st example a alienation that there were some suspicious similarity
reading this could to some properties I that when you get a property from a across the entire property object I when you get the descriptor from the classes with the Holland ties to the object that's because Indian properties however the workings of descriptors they actually implement the full list of the prodigal on what happens that when you the great something with the property by the Susan that the greater to a stored the metal internally on place it under but
said he had a redhead respectively also plasma manholes are descriptors that what they do is
they allow you to how the class suffers from parameter already provide that's where you can call a class middle and you have to explicitly pass the class because there's a scribbling on now blame the greater than what the media asset on the same
copies with setting methods that make sure you can call that function both from an object or a from the cancer from an intense and is going to work I'm probably 1 of the most interesting up a obligations of descriptors our methods because this is the way methods working Python because in the end methods are just functions so let's take a look at that and also all functions are descriptive the how I get method I was doing another in get better these their return the
function applied to an object which is basically saying that the function is converted to a methyl resupplied an option so basically that cities where part 1st aside to prove that methods are just signed and so I find
a class ladies and I create a local method and if i is bigger dictionary of a glass was that
internally method is a class I did which if any stands 0 4 function right and remember from the 1st example what happens when you have something to find the class label them the has a get method but the life us and return that object has yet the steady goes
again if readers that result to now ways information in my work and all that we when you call a method much I
create an object on a method well actually calling that directly because that's the script or so what's companies that is coming to class this keyboard then get felt what's in these times which is object on the class and that following up to the 1st cluster of brain disease that were returned our a method which is uh basically the
construction of that function for the object and then it's a global that accesses I would actually rest of the parameters so in the end have is that this gives of having the well along there from them and the power of Python and maybe guarantees so that we will use in the Brawley really noticing and this is an on solution by which Python is able to watch we transform this a motion which is a function to a metal class and they were the same way as completely and these are the same no among so they know what have
seen our hope I the users descriptors to their bondage but how can we use descriptors to our case to our advantage I'm protein this thing that descriptors hobbies not only other so powerful and there and they allow us to have total control
and do a lot of things but broader they can fix solutions in the goal for example would come right barely greater Rasmussen descriptors In particular fertilizer the greater is the
change the signature of the function so somebody mentioned
that now I have a greater than those who had to transform functions on is going to change their signature in order to make it more are convenience for the user and avoid uh repeated calls so for example I have these resolver that takes some
arguments on with are even cycles front an optical of my domain and there a logical
users my limited is science are made up example was something ladies habits many what frameworks when you actually define your views to receive a request by your by a greater
good so is it will happen is that the greatest change in the signature of the function on the provides you with a with descriptors so there are a but what that change is that duration to the helical as a greater like this 1 that basically exporters the same all 4 parameters as before the Great Silk she I need to and then it goes to the rough function without
object so and change my function to watch really only receive the built should I need to work with and nothing else but who have to keep in mind that the duration of function the 1 that it is a end up being created after the the greater is supplied it still takes of origin of 4 arguments but this example works and if come many functions ladies I can reuse these these logic and applied and this
will work I think that this the fine right but I think you know looking at the cold I find that all had the same situation but this time in a method of a class I try to
apply a greater to learn metal hoping that it will work but actually the ways defined so far
you will not work because remember that method state 1 extra parameter which is self which is the sense of the ocean being
created so as he was having he happening here is that there's a parameter mismatch and everything shift 1 place left so if you want to try to face these the greater my we tend to final resort to like complicated solutions like inspecting the Cold War on inspecting the trace of the frames is set up by actually after seeing help I fell makes functions be able to work as methods were gonna get inspiration
from the EI and use the same technique for this entry to our advantage so basically with aII mind the only thing we have to do is just implement
on the red I may get this decorator also I script a
way do curious I'm only I'm going to the broadened to Python because get metal is going to go the get metal the function being robbed which ultimately lead us not and convert that to method and then I'm going to retrieve yeah this script or by this time with the method itself function and now with this simple change with these 2 lines of call I fixed and the problem was something that would have been otherwise on quite difficult so this is another equal obligation of descriptors on call their their their their use so so basically
to try tool to to wrap up would have seen 1st methods of Escudero's holy war recently each method worried does we have seen that there's like a composition that 1 option to or
object automatically and we're gonna play although she that would have seen I hope by the users descriptors are entirely I'm pretty right to we used to go functions methods and then this idea how we can use descriptors in a similar way of life and ask for the methods so
what is all like to just just shares some final biases which are basically that generally said Lydia to implement assume efforts as possible so from the entire descriptor Braudel which of seen has a 4 matching methods are partial implementation is not only possible is desirable because build she will be of much more I will run faster because it has to do with this checks in runtime said on another important point is that we should like research these for general purpose solution like you to use in descriptors when you actually know not you're creating solution that is abstract enough because this is a feature mostly using frameworks or libraries for example a sky like users
descriptors our for some of the most of the script of analogies there so this is something more and targeted to towards libraries or frameworks or the out something where you will have any API cytokine their way which has a property or something that is what we were
once I encourage you to do so on reserve descriptive when you actually need to to have something that you know many people will apply in different ways so that's all thing URI much for reasoning on how like you if you
question few sold you have any questions they oppose this clear thank you again OK
hello answers which and thank you that was a great talk and you Our can you
recommend a suggest any patterns verbal you would actually use a special like is 3 . 6 feature the some set name the this they want to use that name yeah any any like patterns like what would be a typical example when you want to use it no I think shell and they always have because this could always knows you always have to know the name of the property that is and we modified so sets and there is like I was saying by to always be there is no light you will have to answer or so before they'll always recommend always use it because His mother meander solutions so little mind so if I understood your question
correctly will be like always In the end than the setting thank you any more questions just and find things that are and you have a slide and we've been talking I guess about seed microns sollten you said this is how descriptors work in the Python his that implies that they work differently other Python versions they're actually and I and I would expect there were the same way but for the talk i use of C pythoness reference so I I took a look at the code us as its implementing C Python implementation so the reason why I mentioned see by the specific is because it's a reference I use I don't know if it or our guess yeah because or implementation like by with with the same so up my is will be on the same thank you any more questions OK thank you again it