Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Lazy Attributes and Methods

00:00 As you just saw, the now mocked json module doesn’t have the .dumps() method. The value of using these Mock objects comes in its flexibility,

00:12 so when you want to do something like patch the json module, you don’t want to go ahead and create every single method and attribute that the json module has. That would take forever.

00:23 What you want to be able to do is just mock the bits and pieces of it that you use in your program. Fortunately, you don’t have to do that. The Mock objects are super flexible and they actually create methods and attributes when you access them. So, as you’ve seen, when we printed out the dir() representation of jsonwhich is now a Mock object—it doesn’t have the .dumps() method.

00:50 Let’s go ahead and try an experiment here. Let’s call json.dumps() again after we’ve reassigned json to the mock object.

01:00 Since json doesn’t have a .dumps() method—we don’t see it in this dir() representation—we should probably get some type of exception.

01:10 Let’s go ahead and see if we can print(data).

01:16 Go down and clear the screen, run our program.

01:21 And that’s interesting—we actually get another Mock object. What happened here is that we called this .dumps() method, which didn’t exist, and when we called it, it actually created another Mock object.

01:37 So obviously, this isn’t really the true .dumps() method, and we can verify that by even deleting the arguments. So, the regular json.dumps() requires some arguments but if we save this and we call json.dumps() with no arguments, we run our program again—we’ll see that it still returns a Mock object and it doesn’t raise an exception or cause any error.

02:03 And this will accept any arguments that you want to pass to it. I could pass in, you know, whatever. Save that, run it again. We get a Mock object, no exception.

02:16 So the fact that when you create a method like .dumps() from a Mock object it will return another Mock object and it will accept any arguments that you pass to it—this feature of Mock objects is what allows a lot of flexibility and the ability to create more control and testing. And in the next video, we’re going to explore this idea some more.

Become a Member to join the conversation.