text stringlengths 1 81 | start float64 0 10.1k | duration float64 0 24.9 |
|---|---|---|
and let's require that in order to set | 4,961.44 | 4 |
some attribute you go through some | 4,963.6 | 4.599 |
function and conventionally those | 4,965.44 | 5.44 |
functions are called a getter function | 4,968.199 | 5.04 |
and a Setter function and why are we | 4,970.88 | 4.4 |
using functions or in this case methods | 4,973.239 | 4.361 |
inside of a class well once you have | 4,975.28 | 4.16 |
functions those are just actions or | 4,977.6 | 3.2 |
verbs that you and I can create | 4,979.44 | 3.32 |
ourselves we can put any error | 4,980.8 | 4.359 |
correction I want in these functions | 4,982.76 | 3.84 |
because it's code that's going to get | 4,985.159 | 4.601 |
executed top to bottom so how can I now | 4,986.6 | 7.079 |
prevent the user from setting the house | 4,989.76 | 6.919 |
to an invalid value let me borrow some | 4,993.679 | 5.841 |
logic from before rather than blindly do | 4,996.679 | 5.96 |
this just set self. housee equal to that | 4,999.52 | 5.159 |
house value that's passed in let's add | 5,002.639 | 4.841 |
our error checking there so if house is | 5,004.679 | 6.56 |
not in the following list of Gryffindor | 5,007.48 | 4.96 |
or | 5,011.239 | 7.161 |
Hufflepuff or slyther or Ravenclaw or | 5,012.44 | 8.759 |
Slytherin just as before let's go ahead | 5,018.4 | 5.16 |
and raise a value error just to signify | 5,021.199 | 4.081 |
that uhuh something has gone wrong I'll | 5,023.56 | 3.96 |
be more explicit I'll include a message | 5,025.28 | 5.359 |
like invalid house quote unquote | 5,027.52 | 5.4 |
otherwise I'm going to proceed on now | 5,030.639 | 5.6 |
line 21 to set self. house to house so | 5,032.92 | 5.719 |
I've just copied if you will or retyped | 5,036.239 | 4.241 |
my error checking inside of this | 5,038.639 | 4.441 |
so-called Setter function now why have I | 5,040.48 | 5.28 |
done that well to be clear whenever the | 5,043.08 | 5.24 |
user or the programmer writes code like | 5,045.76 | 6.479 |
this student.h housee equals what's | 5,048.32 | 7.2 |
about to happen magically is python will | 5,052.239 | 4.96 |
not just let the programmer access | 5,055.52 | 4.24 |
student.h house directly that attribute | 5,057.199 | 5.721 |
that instance variable AKA self. housee | 5,059.76 | 5.439 |
it's instead going to magically | 5,062.92 | 5.04 |
automatically call this Setter function | 5,065.199 | 5.48 |
for me how does python not to do that | 5,067.96 | 4.92 |
well if it sees that on the left hand | 5,070.679 | 6.081 |
side there is self. house where house is | 5,072.88 | 7.44 |
the name of the getter or Setter and | 5,076.76 | 5.76 |
then it sees an equal sign indicating | 5,080.32 | 4.16 |
assignment that's just enough of a | 5,082.52 | 3.679 |
visual clue to say wait a minute I'm not | 5,084.48 | 3.84 |
going to let you access that attribute | 5,086.199 | 5.04 |
directly I'm going to use the setter | 5,088.32 | 4.879 |
instead why because the equal sign means | 5,091.239 | 3.681 |
I'm trying to set I'm trying to assign a | 5,093.199 | 4.121 |
value from right to left into that | 5,094.92 | 4.52 |
attribute so what Python's going to do | 5,097.32 | 4.359 |
automatically is call this function for | 5,099.44 | 4.52 |
me and that's amazing because now I can | 5,101.679 | 5.361 |
execute code and algorithm to check do I | 5,103.96 | 6.12 |
want to let the user the programmer set | 5,107.04 | 5.8 |
that attribute to that value if not I'm | 5,110.08 | 4.2 |
going to raise a value error and you're | 5,112.84 | 4 |
just not going to be able to do it if so | 5,114.28 | 5.04 |
fine I'll go ahead and set it for you | 5,116.84 | 4.12 |
but in order to do this we need a little | 5,119.32 | 3.28 |
more syntax and I'm going to get rid of | 5,120.96 | 3.44 |
my comment and I'm going to use that | 5,122.6 | 4.519 |
decorator I need to tell python to treat | 5,124.4 | 5.6 |
this method as a getter and then the | 5,127.119 | 4.401 |
Syntax for the setter is a little | 5,130 | 4.639 |
different you now say house. Setter I | 5,131.52 | 5.199 |
wish one was getter and the other was | 5,134.639 | 4.04 |
Setter that's not the way they designed | 5,136.719 | 4.52 |
it when you want to define a getter you | 5,138.679 | 5.641 |
just say at property above the function | 5,141.239 | 6.241 |
and you name the function exactly like | 5,144.32 | 4.879 |
you would like the property to be called | 5,147.48 | 5 |
quote unquote house once you do that you | 5,149.199 | 5.321 |
can now use a new decorator that's sort | 5,152.48 | 4.12 |
of automatically created for you called | 5,154.52 | 4.36 |
at house because I called it house and | 5,156.6 | 4.84 |
then you literally say at house. Setter | 5,158.88 | 5.52 |
and this whole line on line 17 is a clue | 5,161.44 | 5.679 |
to python that here comes a function | 5,164.4 | 5.319 |
whose name is identical but notice that | 5,167.119 | 4.721 |
it takes two arguments both self so you | 5,169.719 | 3.52 |
have access to the contents of the | 5,171.84 | 3.359 |
object and house which is just going to | 5,173.239 | 4.48 |
be a stir that comes from the programmer | 5,175.199 | 5.561 |
from like the human input return value | 5,177.719 | 6.201 |
so that you can set that value as well | 5,180.76 | 5.919 |
but there's one fix I need to make now | 5,183.92 | 5.16 |
here everything else I think is still | 5,186.679 | 6.121 |
good however watch this I no longer need | 5,189.08 | 8 |
this error check here why because if I | 5,192.8 | 6.76 |
scroll back down to my code here I | 5,197.08 | 5.88 |
claimed a moment ago that code like this | 5,199.56 | 6.76 |
with uh student.h house equals is going | 5,202.96 | 5.44 |
to automatically get python to call my | 5,206.32 | 5.879 |
Setter for me guess what even up here in | 5,208.4 | 6.839 |
in my init method calling self. housee | 5,212.199 | 6.281 |
equals is also going to call my Setter | 5,215.239 | 6.041 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.