Special Methods7:30 with Kenneth Love
We haven't talked about access control yet. Does Python even have it?
If you remember nothing else from this video, remember: We're all adults here. Even if you're not legally an adult, in the Python community, you're expected to behave like one and you'll be treated like one, at least as far as code goes.
There is no foolproof way of protecting your code from outside use in Python. But if you follow the conventions in this course, you won't have to. Most Python programmers see a method or attribute prefixed with
_ and leave it alone. That goes doubly so for methods or attributes with a double underscore preceding but not trailing (
If you study object oriented programming and many other languages, 0:00 one of the first things you'll learn about is hiding elements of your classes away. 0:03 In Python, we have a motto of we're all adults here. 0:06 We expect people to use whatever attributes are in a class responsibly. 0:09 We do have a few ways of locking information away, though. 0:13 Let's go check them out in work spaces. 0:16 Let's talk a bit more about this, we're all adults attitude. 0:19 Most of the time if you have an attribute or 0:22 a method that you don't want people to use, you just prepin the name with 0:24 an underscore, right like _don't_use, and people will leave it alone. 0:27 Any method or attribute that starts with an underscore, 0:32 tells me and most other experience Python developers 0:34 that it shouldn’t be used by the outside world, and now it will tell you that too. 0:37 But what if you want to make sure that people can’t use your method or 0:42 your attribute. 0:45 Well, what’s better than one protection? 0:46 Two protections. 0:49 Double underscores make things pretty much inaccessible outside of the class. 0:50 Now, that pretty much is important, you can get to them but 0:53 you have to know the special trick. 0:56 So, I've created a file called protected dot pie and 0:58 I'm going to make a new class here called protected. 1:01 And inside of here I'm going to make an attribute named thunder name, but 1:04 notice that there's no trailing thunder. 1:07 So there's an underscore only at the beginning. 1:11 There's no underscores at the end. 1:15 And then I'm going to make a method that follows that same convention. 1:17 I'm just going to call it method. 1:20 And it's going to return self.__name. 1:22 All right so let's try that one out and are python shell. 1:27 From protected import Protected. 1:36 And I'm gonna call this proct, or I'll say prot. 1:40 That makes more sense. 1:43 And that is an instance of protected. 1:45 Okay, so far nothing's different. 1:49 But let's try doing prot.__name doesn't it have that attribute. 1:51 Okay, what about __method. 1:57 I can call that one right? 2:00 That one doesn't exist either. 2:02 Obviously they're there. 2:04 I can see them on the codes. 2:05 I know they're there. 2:06 But I can't get to them outside of the class. 2:07 If I really need to, though, I can figure out how to get to them. 2:10 Let's do dir, D-I-R, which shows the directory of an object. 2:14 So let's try that on prot and see what we can find. 2:20 And look right here, we have double underscore method, and double underscore 2:23 name right there, but they have something special in front of them. 2:27 So this is called name mangling and Python does this to anything that starts with 2:30 a double underscore and does not end with a double underscore. 2:34 And it does that to make it to where it's harder to get access to that thing. 2:38 But I can most certainly do prot._Protected_method() and 2:42 it'll print out 'Security' because inside of the class, this still works. 2:49 This doesn’t get mangled, right? 2:53 Inside of the class still has reference, still has the ability to reference it. 2:55 Outside of the class, does not. 2:59 And I can, just for, Just for 3:02 covering everything, I can also get to name and now that name has changed. 3:07 Now, in Python, nothing is ever truly locked away. 3:12 Most people, though, are perfectly happy to just work with what you give them. 3:14 You can lock things away like that with this double underscore. 3:17 Often though, you do need to lock away a few things. 3:21 Usually, that's when you need to lock away how an attribute is set, or 3:27 how it's retrieved. 3:31 Because you have things that need to be calculated. 3:32 We can do this using a concept known as a property. 3:35 Let's make a new file called circle.pi. 3:38 And instead of circle.pi, I'm gonna make a class called circle. 3:42 And let's go ahead and set up init and we'll say self and diameter 3:46 and we're gonna say self.diameter = diameter, whatever gets passed in. 3:53 And then we're gonna say, radius, and 3:59 we're gonna return self.diameter / 2. 4:04 The radius of a circle is half of its diameter. 4:07 If we want people to use this right now, they can call circle.radius and 4:09 they'll get the radius, right? 4:13 They'll have to use the parenthesis and it's a method, and 4:15 they have to just use it. 4:17 Now, if we don't want people to know that it's a method, and 4:19 we want to just return this as being another value on the class, 4:22 another attribute, we can decorate this with property. 4:27 So we can say that radius is a property of our circle class. 4:31 And now we can use those property anywhere that we have a circle instance. 4:34 Let's just do it in here. 4:39 So small = circle, and we're going to say the diameter is ten. 4:41 So let's print(small.diameter) and then let's print(small.radius). 4:47 And let's go ahead and run this. 4:55 So python circle.py. 4:56 And we get that our diameter is 10 and our radius is 5.0, cool. 5:02 That's what we expected, right? 5:07 Now, that's really handy when we just need new ways to represent data, or 5:10 like this calculate a value based on some other value. 5:13 Now, notice too that I didn't have to call radius, 5:16 I didn't have to use the parenthesis there, it's because properties act 5:19 like attributes, but they have one important difference from attributes. 5:23 So they're an attribute when you want to get to them, right, 5:28 you just small.radius and you get the radius. 5:31 But I can't do small.radius = 10. 5:33 I can't double the size of the circle, Because now I get an attribute error. 5:38 I can't set the attribute. 5:43 Now, that's weird, because I can set attributes. 5:45 But because radius is a property, it doesn't know how to set it. 5:48 Python doesn't know what to do with this. 5:51 So, we can work around this, though. 5:53 We can provide what's known as a setter 5:57 in order to set the attribute that the property represents. 6:00 So, let's go ahead and do that. 6:05 First thing, we have to do is we have to decorate this one. 6:08 And we tell it what property we're decorating. 6:11 And then we say this is the setter method for doing that. 6:14 And then this has to have the same name as the other method. 6:18 And it takes self, and then we're going to take the new radius, right? 6:24 And we're gonna say self.diameter, =radius * 2. 6:27 I've always found this syntax to be a little weird. 6:33 Since we have a property for radius, we use the property name and then .setter 6:37 to mark another method as to what to run when the property is assigned a value. 6:41 We have to name both method the same. 6:44 And now, we have both a getter and a setter, methods that get a value or 6:47 set a value for our radius property. 6:51 So let's play with that a little bit, so we'll do that and 6:55 then let's just print small.diameter again, right? 6:59 And python circle.py. 7:03 And now, our diameter's back to 20, or is up to 20 cuz we doubled it, awesome. 7:06 Now, you might not find yourself using the leading double underscore trick, 7:12 privatizing variables very often. 7:16 You might not use the property setter syntax very often either. 7:18 Both of these are really handy tricks to know though, when you need them. 7:21 I'm certain you'll use the property quite often though. 7:24 It's one of the handiest tools in Python's toolbox. 7:27
You need to sign up for Treehouse in order to download course files.Sign up