All language subtitles for 4. Methods Part 1

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (SoranĂ®)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 1 1 00:00:05,600 --> 00:00:09,260 an important part of object-oriented programming is the concept of 2 2 00:00:09,260 --> 00:00:14,880 encapsulation so the idea here is that objects contain the data and the methods 3 3 00:00:14,880 --> 00:00:19,720 that operate on that data and don't expose the actual implementation to the 4 4 00:00:19,720 --> 00:00:24,960 outside world so it's worth noting that object oriented programming is not the 5 5 00:00:24,960 --> 00:00:30,050 only way to achieve this and languages such as modular 2 aimed to 6 6 00:00:30,050 --> 00:00:36,420 provide encapsulation by using modules rather than objects modular 2 enhance to 7 7 00:00:36,420 --> 00:00:41,080 become modular 3 which was really quite influential in the design 8 8 00:00:41,080 --> 00:00:46,670 of Java, C++ and also Python although the object approach to encapsulation was 9 9 00:00:46,670 --> 00:00:51,380 the only one adopted by Java Python allows us to encapsulate data methods 10 10 00:00:51,380 --> 00:00:56,050 using either an object orientated paradigm or a modular approach and we 11 11 00:00:56,050 --> 00:01:00,190 saw the modular approach when using the pytz module to handle time zone 12 12 00:01:00,190 --> 00:01:05,690 information earlier in this course so you don't have to know how pytz was 13 13 00:01:05,690 --> 00:01:08,080 implemented in order to use it 14 14 00:01:08,080 --> 00:01:12,180 the method that provides and the time zone information it uses are encapsulated 15 15 00:01:12,180 --> 00:01:18,730 within the pytz module in fact pytz also uses classes internally so it's possible 16 16 00:01:18,730 --> 00:01:23,590 to mix both methods quite happily now I repeatedly pointed out that everything 17 17 00:01:23,590 --> 00:01:28,560 in Python is an object so it may seem strange that a language that is really 18 18 00:01:28,560 --> 00:01:34,670 so heavily based on objects doesn't restrict us to using an object approach this is 19 19 00:01:34,670 --> 00:01:39,950 one of the important design decisions of the Python language the basic principle 20 20 00:01:39,950 --> 00:01:44,860 being that we're all responsible adults at least when programming now I mention this 21 21 00:01:44,860 --> 00:01:49,640 because many languages work by imposing restrictions on what can be done with 22 22 00:01:49,640 --> 00:01:55,320 encapsulated data Java and C++ for example restrict access to class fields 23 23 00:01:55,320 --> 00:01:59,930 by allowing them to be marked as private in Python you can indicate that a 24 24 00:01:59,930 --> 00:02:03,740 method or attribute should be considered non public but it's extremely 25 25 00:02:03,740 --> 00:02:08,460 difficult to fully enforce this once you've been introduced to the idea of 26 26 00:02:08,460 --> 00:02:12,610 encapsulation it can be tempting to spend a lot of time trying to enforce it 27 27 00:02:12,610 --> 00:02:13,470 in Python 28 28 00:02:13,470 --> 00:02:17,620 especially if he used 2 other languages such as Java that do a reasonably good 29 29 00:02:17,620 --> 00:02:22,410 job of preventing access to private data but you be more productive if you accept 30 30 00:02:22,410 --> 00:02:25,070 that Python takes a different approach 31 31 00:02:25,070 --> 00:02:28,360 neither approach is necessarily better they are just different and each has its own 32 32 00:02:28,360 --> 00:02:33,260 advantages and disadvantages so let's continue our introduction to classes in 33 33 00:02:33,260 --> 00:02:37,690 Python with another example we are going to look at the concept of a bank account and 34 34 00:02:37,690 --> 00:02:42,340 the classes are going to enable money to be deposited withdrawn and will hopefully 35 35 00:02:42,340 --> 00:02:46,660 anyway maintain the correct balance for the account so I'm going to use the same 36 36 00:02:46,660 --> 00:02:50,450 file we use previously but I'm just going to create a new Python file so we are going call this one 37 37 00:02:51,340 --> 00:03:01,130 accounts....so let's start first by creating the class 38 38 00:03:01,130 --> 00:03:08,640 so we are gonna call the class account so.... and it's going to be a simple 39 39 00:03:08,640 --> 00:03:11,990 .... 40 40 00:03:15,200 --> 00:03:27,650 .... 41 41 00:03:27,650 --> 00:03:34,370 ....and incidentally the convention is to start class names with a capital letter and to use 42 42 00:03:34,370 --> 00:03:38,790 camel case being that each time you got a different word you capitalized 43 43 00:03:38,790 --> 00:03:44,710 just that word so if we're calling this the account number class we would type something like that 44 44 00:03:44,710 --> 00:03:46,430 capitalize each 45 45 00:03:46,430 --> 00:03:51,090 word but basically start with a capital letter so that is the convention in Python so 46 46 00:03:51,090 --> 00:03:55,730 ok will continue on with our init method so we want to passed name and balance to 47 47 00:03:55,730 --> 00:04:04,270 it and we are going to assign the attributes.... 48 48 00:04:04,270 --> 00:04:16,990 ..... 49 49 00:04:16,990 --> 00:04:22,370 .... 50 50 00:04:22,370 --> 00:04:30,230 ...and we are going to just check the amount that is passed 51 51 00:04:30,230 --> 00:04:39,960 to make sure that its greater than 0...and only add it to our balance if its greater than 0 and we will add the amount to what 52 52 00:04:39,960 --> 00:04:49,330 the current balance was like so....next let's do a withdraw method.... 53 53 00:04:49,330 --> 00:04:56,510 ... 54 54 00:04:56,510 --> 00:05:23,400 .... 55 55 00:05:23,400 --> 00:05:31,970 .....ok so there's our class and as I just mentioned the convention is to 56 56 00:05:31,970 --> 00:05:37,320 start class names with a capital letter and and to use camel case with that said you'll find 57 57 00:05:37,320 --> 00:05:41,880 many the built-in classes in Python do use lowercase nevertheless IntelliJ will 58 58 00:05:41,880 --> 00:05:45,390 give you a warning if you use lowercase or separate paths of the class name with 59 59 00:05:45,390 --> 00:05:58,100 underscores...another warning as you can see should only use case convention it's not 60 60 00:05:58,100 --> 00:06:05,220 happy if we use underscores now I've included a dot string on line 2 at the start of the class and it's a good idea to do 61 61 00:06:05,220 --> 00:06:09,620 this with all your classes although it can appear to be a bit redundant with simple classes like 62 62 00:06:09,620 --> 00:06:14,400 this one which he can really figure out the functionality just by glancing it's 63 63 00:06:14,400 --> 00:06:17,410 a good habit to get into and once your class structures get more complicated 64 64 00:06:18,230 --> 00:06:21,910 you will be glad that you did do this and so will anyone else who uses your classes 65 65 00:06:21,910 --> 00:06:27,160 and I'll be talking more about doc strings later when I show you what they used for in 66 66 00:06:27,160 --> 00:06:31,790 more detail now the init method as you recall is used to initialize the 67 67 00:06:31,790 --> 00:06:36,540 class and it's extremely rare to create a class without providing an init method 68 68 00:06:36,540 --> 00:06:41,530 now in an earlier video I used the term constructor to described the init method but 69 69 00:06:41,530 --> 00:06:46,210 really that's not strictly true in the new style Python classes that will be 70 70 00:06:46,210 --> 00:06:51,700 using for most of this section the Class creation process involves two steps so 71 71 00:06:51,700 --> 00:06:56,380 the first method to be called when a class instance is created is new and this 72 72 00:06:56,380 --> 00:07:01,450 takes care of the actual creation the init method then customizes the instance 73 73 00:07:01,450 --> 00:07:07,100 performing tasks such as giving values to the data attributes for example so 74 74 00:07:07,100 --> 00:07:11,890 technically the class constructor is the new method which is actually calling 75 75 00:07:11,890 --> 00:07:16,050 underscore underscore new underscore underscore as you probably expect 76 76 00:07:16,050 --> 00:07:20,580 generally speaking you don't need to define new except in special cases 77 77 00:07:21,160 --> 00:07:25,200 when subclassing certain types of classes so I won't be discussing you 78 78 00:07:25,200 --> 00:07:29,610 much in this section as a result now old style classes were the only type 79 79 00:07:29,610 --> 00:07:34,750 of class available in Python 2 up until 2.2 when the new style classes are 80 80 00:07:34,750 --> 00:07:40,050 introduced so in Python 2.2 onwards you could use both types of 81 81 00:07:40,050 --> 00:07:44,700 classes I will discuss the Python 2 classes later on 82 82 00:07:44,700 --> 00:07:48,980 so that your aware of the differences and can recognize them but in Python 3 83 83 00:07:48,980 --> 00:07:52,760 there are only the new style classes and as a results I'm going to be 84 84 00:07:52,760 --> 00:07:57,290 concentrating on those as we move forward so getting back to the account 85 85 00:07:57,290 --> 00:08:01,730 class the init method takes two parameters in addition to self and these 86 86 00:08:01,730 --> 00:08:07,300 are used to set the values of the data attributes name and balance now notice that all 87 87 00:08:07,300 --> 00:08:10,480 the methods takes self as their 1st parameter and you saw IntelliJ 88 88 00:08:10,480 --> 00:08:14,270 automatically adding that for us and whenever we want to refer to the data 89 89 00:08:14,270 --> 00:08:18,350 attributes we have to use the form self. and whatever the name of the attribute is 90 90 00:08:18,350 --> 00:08:23,980 again as you see me type in this class in Python there is no shorthand and self 91 91 00:08:23,980 --> 00:08:28,640 must be specified if your used to java you can omit the word this before 92 92 00:08:28,640 --> 00:08:32,980 a field if their references is not ambiguous but in Python you can't do that so you need 93 93 00:08:32,980 --> 00:08:37,330 to get into the habit of doing that each time I just wanna remember i just fixed that 94 94 00:08:37,330 --> 00:08:44,050 those warnings and add the extra space to keep IntelliJ happy now the positive withdrawal methods 95 95 00:08:44,050 --> 00:08:48,370 obviously a pretty simple and they adjust the balance by the amount passed to the parameter 96 96 00:08:48,370 --> 00:08:53,160 they also check that the amounts are positive although as withdrawal its just a negative 97 97 00:08:53,160 --> 00:08:57,410 deposit and a deposit is just a negative withdrawal we could have used a single 98 98 00:08:57,410 --> 00:09:01,890 method to handle both cases now there are extra steps that are really sure of 99 99 00:09:01,890 --> 00:09:05,010 implemented in the withdrawal method to make sure there is enough money in the 100 100 00:09:05,010 --> 00:09:09,550 account though so it makes sense to keep the two transaction type separate the 101 101 00:09:09,550 --> 00:09:14,330 show balance method again very simple it just prints the balance data attribute so now the 102 102 00:09:14,330 --> 00:09:17,860 that the classes been defined we can create accounts with people and start 103 103 00:09:17,860 --> 00:09:22,160 looking out for their money so I'll start by creating account for myself 104 104 00:09:22,160 --> 00:09:27,400 of course with a zero balance and then deposit withdraw some cash let's go ahead and do 105 105 00:09:27,400 --> 00:09:29,120 that 106 106 00:09:29,120 --> 00:10:00,400 .... 107 107 00:10:00,400 --> 00:10:09,710 .....and lets 108 108 00:10:09,710 --> 00:10:15,620 run this to make sure its workings account created for Tim the original balance was 109 109 00:10:15,620 --> 00:10:21,080 0 which would expect balances is a thousand when we deposited 1000 and we've 110 110 00:10:21,080 --> 00:10:26,220 withdrawn 500 and shows a balance of 500 so that's working fine 111 111 00:10:27,300 --> 00:10:30,610 so this is a very simple example as I said and theirs plenty of room for improvement 112 112 00:10:31,760 --> 00:10:35,190 now rather than having to call the show balance method after each transaction 113 113 00:10:35,190 --> 00:10:39,330 will probably be helpful if the balance if the deposit and withdrawal methods 114 114 00:10:39,330 --> 00:10:45,210 did this for us automatically I will also reluctantly fix the problem that lets me take as much 115 115 00:10:45,210 --> 00:10:49,520 money as I want regardless of the actual balance so for both methods I'm just gonna 116 116 00:10:49,520 --> 00:10:53,350 call the show balance method after adjusting the balance preventing 117 117 00:10:53,350 --> 00:10:57,710 unlimited withdrawals easily done just to confirm the way the withdrawal method is 118 118 00:10:57,710 --> 00:11:04,510 written at the moment I could quite happily withdraw 50,000 and it would 119 119 00:11:04,510 --> 00:11:09,100 work all be with a $49,000 overdrawn balance but the point is it would work and we need 120 120 00:11:09,100 --> 00:11:13,210 to restrict that so that when we are withdrawing money we can only withdraw the 121 121 00:11:13,210 --> 00:11:18,760 amount to the maximum amount we've got currently in the account so lets go ahead and do that so 122 122 00:11:18,760 --> 00:11:24,800 we are going to add to the deposit method so..... 123 123 00:11:24,800 --> 00:11:30,780 ... 124 124 00:11:30,780 --> 00:11:45,310 .... 125 125 00:11:46,260 --> 00:11:49,630 .... 126 126 00:11:51,210 --> 00:12:10,490 .... 127 127 00:12:10,490 --> 00:12:16,740 ....so we'll leave the initial show balance call on line 27 128 128 00:12:16,740 --> 00:12:22,450 but will comment out the other one because they can be called automatically 129 129 00:12:22,450 --> 00:12:27,400 when we call those methods and lets put a valid withdrawal if we went back into 500 130 130 00:12:27,400 --> 00:12:36,570 then we will put a invalid one in.....so lets run this one 131 131 00:12:36,570 --> 00:12:44,020 you can see balance is 0 account was created for 132 132 00:12:44,020 --> 00:12:45,860 Tim balance is 0 133 133 00:12:45,860 --> 00:12:52,700 balance is 1000 it is calling the show balance method now as we are calling the deposit 134 134 00:12:52,700 --> 00:12:56,070 and withdrawal methods balance is deducted to 500 and we got a message 135 135 00:12:56,070 --> 00:13:02,320 here when we called line 34 when line 34 was executed the amount must be 136 136 00:13:02,320 --> 00:13:06,640 greater than 0 no more than your account balance and it also reprinted the 137 137 00:13:06,640 --> 00:13:09,880 balance you can see the balance is still the same as it was when that method 138 138 00:13:09,880 --> 00:13:13,830 was called so this seems to be working fine and it does show that you can 139 139 00:13:13,830 --> 00:13:18,070 call methods from other methods as I mentioned earlier that you must prefix 140 140 00:13:18,070 --> 00:13:22,350 all attributes with self and that applies to method calls in this class and you 141 141 00:13:22,350 --> 00:13:27,050 probably saw when I was typing one of the show balance methods that I forgot to put the self their and 142 142 00:13:27,050 --> 00:13:32,790 IntelliJ was flagging as an error so this may catch Java programmers out for 143 143 00:13:32,790 --> 00:13:36,650 a while and it shouldn't be a huge problem because as you saw IntelliJ will let you 144 144 00:13:36,650 --> 00:13:41,910 know as soon as you tried to use attribute or a method call within a method without using 145 145 00:13:41,910 --> 00:13:49,870 self again just to confirm what I'm talking back we can delete that we then get an error un-resolve 146 146 00:13:49,870 --> 00:13:56,490 reference so we have to use self and likewise for the attributes we get an error 147 147 00:13:56,490 --> 00:13:59,840 unless you specifically do it so It will probably catch you out if your coming from another 148 148 00:13:59,840 --> 00:14:02,710 language such as Java but after you 149 149 00:14:02,710 --> 00:14:07,240 done a bit of Python coding it becomes pretty straightforward and 150 150 00:14:07,240 --> 00:14:12,120 again IntelliJ is helping you by flagging the error so you need to fix the 151 151 00:14:12,120 --> 00:14:15,930 errors to run the program so sooner or later you'll figure it out anyway but 152 152 00:14:15,930 --> 00:14:23,270 with that said there is a fundamental problem with the code for this account class and 153 153 00:14:23,270 --> 00:14:26,500 although fixing it is easy their more than one way to do so 154 154 00:14:26,500 --> 00:14:30,620 I'm gonna cover that later but for now I'm gonna see if you can spot the problem 155 155 00:14:30,620 --> 00:14:34,070 until we get to that section where our I will show you how to get around it 156 156 00:14:34,070 --> 00:14:36,480 so we'll start working on that in the next video 18372

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.