Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Method Arguments5:33 with Kenneth Love
Methods, like functions, are better with arguments. Let's see how to make our methods take parameters other than just the instance they're called on.
All of the same rules apply to method arguments as they do to function arguments, with the exception that the first argument has to represent the instance. You can use default values,
**kwargs all you want!
setattr() usage in this video catch you off-guard? Let's talk about what it is and what it's doing.
First, read the docs about it. I know documentation can sometimes be hard to read, but getting in the habit of reading it, starting there, will help you a lot in the long run.
Now, let's try an experiment. I have a class:
class Animal: def __init__(self, **kwargs): self.species = kwargs.get("species") self.age = kwargs.get("age") self.sound = kwargs.get("sound")
**kwargs here to pack any and all keyword arguments into a dictionary. And then I'll make a wolf using that class.
>>> wolf = Animal(species="Canus Lupus", age=5, sound="howl", color="gray") >>> wolf.species "Canus Lupus"
Great. No errors, Python's happy, I have an instance of my
Animal class. In the
__init__, I set three attributes:
sound. I didn't set
color, though. Is it available?
>>> wolf.color AttributeError...
No, it's not! But there weren't any errors when I created the instance. Why isn't the attribute available?
Well, because I didn't create it. Remember, Python is very explicit. If I don't do something explicitly, Python doesn't do it at all. Since I didn't assign a
color attribute to
self, my instance doesn't have a
This is where
setattr comes in.
setattr lets me set attributes that I don't know about beforehand. I can, of course, go back and explicitly define a
color attribute but what if the next user comes along and she wants a
weight attribute? A year from now,
Animal.__init__ might be hundreds of lines long with attribute declarations! And some of those attributes will only apply to a few animals!
But...if I'm clever and use
setattr, I can just happily accept any and all attributes that someone gives for a particular
Animal instance without having to continually update
class Animal: def __init__(self, **kwargs): for attribute, value in kwargs.items(): setattr(self, attribute, value)
So, since I have a handy-dandy
kwargs dict due to using
**kwargs in the method parameters, I can loop through it's
items method and pull out the two values from each iteration into
setattr function takes three arguments: the object to work on, the attribute name to define, and the value to give that attribute. Now, no matter what attribute values I give to my object (assuming they're valid attribute names), my class will happily apply them.
So what's the sneakier way than using
setattr()? Every object has an attribute named
__dict__ that is a dictionary representation of the writable attributes of an object. You can update dictionaries so you could do:
class Thief: def __init__(self, name, **kwargs): self.name = name self.__dict__.update(kwargs)
Usually, though, you don't want to update
.__dict__ directly as it's mostly meant to be a read-only resource. Just because you can write to it doesn't mean you should!
I'm pretty sure you could guess how this works.
But let's talk a bit about how to make methods that take parameters
other than self.
Python doesn't like to surprise us and definitely doesn't hear.
Just like with standard functions,
we can add any number of parameters to our methods.
We can use args and kwargs too if we want.
So, let's add a new method down here and let's call this hide.
And lets take self, of course, an instance, and lets take a new argument
which is light_level, right, because it's easier to hide in the dark, and
we're going to return if self.sneak is equal to true, or sneaky rather,
and that the light_level is less than ten.
Okay? So, if the light_level's is less than ten,
you can always hide so long as you're a sneaky thief.
Sort of the rules of our game.
All right, so let's go and try this again in our shell.
And let's make Thief.
All right, so now let's try kenneth.hide when the white level's (4).
Cool, and that's expected because I am sneaky and
the light level is below ten, okay?
Let's try kenneth.hide and the light level is 14.
And that one comes out as false, okay?
And then, if we do kenneth.sneaky equals false and kenneth.hide and
it's pitch black at zero, false.
That's working just like I expected.
Let's talk about a special method though and one that you'll likely use a lot.
When you create a new instance of a class, Python looks for
a method named double underscore init double underscore.
The double underscores, or as I call them, dunders, on both sides,
indicate that the method is the one that Python will run on its own and
that we don't usually need to worry about.
At least we don't have to use it explicitly,
we don't have to call it ourselves.
Now, this method lets us control how the classes are created or
initialized, which is why it's called init.
Let's make our Thief a little smarter in its creation.
So, let's add def, init, and there's those double underscores.
And this'll take self, it'll take a name, it'll take a sneaky argument.
We'll automatically set to True, and
it'll take any other kwargs that happen to come in, any other keyword arguments.
So, we'll say self.name = name, and we'll say self.sneaky = sneaky.
All right? And everything else,
we're gonna leave alone.
And we could actually make this a little easier here.
We could say, instead of checking the self.sneaky,
we could return self.sneaky and that one.
So, that way our two methods are pretty similar to each other.
So, we have a couple of new things in this one.
Let's try it out.
Okay, back into Python from characters import Thief,
kenneth equals Thief, and we forgot to write a name, okay?
So, let's add a name under this.
So, I'm gonna add in Kenneth, and I'm gonna go ahead and say false for
the sneaky part.
So now, let's do kenneth.name.
That gives me back Kenneth, nice.
And if we do kenneth.sneaky, we get back false.
So, when I created the instance, I passed two arguments,
which Python plopped into the two arguments in the init method.
Then we assigned each of those to a different attribute on the instance,
by using self, right?
self.name = name, self.sneaky = sneaky, that's awesome, okay.
So, what about this kwargs thing?
Well if you remember, **kwargs is the dictionary full of key value pairs.
Let's loop through that and assign all of those things to the instance.
So, down here below that, let's do four key value in kwargs.items,
and then we'll use a new method, or a new function rather called setattr,
and let's specify the instance to set it on,
we attribute the set and the value to get to that attribute.
So, the set out of function is amazingly useful in situations like this because we
don't know the name of the attributes that we need to create.
So, said do that with this random values that come in this users supplied values.
There is a sneakier way of doing this but is less clear.
So, I tend to avoid it.
I’ve put it in the teachers notes though if you’re curious.
But, for now let’s try this out.
Pull our console back up.
From characters, import thief.
Kenneth equals thief.
I keep forgetting to supply that name.
So, we'll do Kenneth, we'll leave sneaky alone.
We'll say scars=None, and
So, if I do kenneth.name, we get back Kenneth, If I do kenneth.sneaky,
I should get True, because that's what we set as the default.
And if I do kenneth.favorite_weapon, I get Wit, cool.
So, it looks like that works and that lets us add extra information to our instances.
You're doing great with building simple custom classes and
using attributes and methods.
You'll find yourself using that set outer pattern, so
make sure you add it to your notes and practice it a few times.
In fact, let's do a code challenge or two around it.
Then come right back for a serious talk about one of the hardest parts of object
oriented programming, design.
You need to sign up for Treehouse in order to download course files.Sign up