Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Exploring Possible Protocol Members

00:00 Up until this point, your protocols only had methods, but you can specify other types of members to allow you to define more flexible and/or complex protocols.

00:12 So the different types of members you can have include class and instance attributes, and you will see how to distinguish both in your protocol. You can have instance methods, which are the things you’ve been using so far, swim, dive, etc.

00:28 You can have class and static methods. You can have properties, and you can even have abstract methods. And what you will do now is take a look at a demonstration protocol that specifies all of these different types of members.

00:44 Now go ahead and create a new file called members.py, where you will define a protocol with all these different types of members.

00:54 Once you have your file members.py, the very first thing you need to do is import Protocol from the module typing.

01:02 After all, you are defining a protocol. And what you’ve seen up until now is a protocol, let’s call this protocol MembersDemo. You will be cramming lots of different types of members into this one protocol so let’s keep the names generic.

01:19 And since this is a protocol, it needs to inherit from Protocol and what you’ve seen so far is that you can have instance methods, which really are just regular methods, that might have some arguments that typically you also type, and a return value. ..., the ellipsis is because you don’t have to commit to any particular implementation.

01:42 These are instance methods. Now you can also have class and instance attributes. Go ahead and let’s create a class attribute. Let’s say it’s an integer, for example, and an instance attribute, that’s a string.

02:01 Now the way you distinguish both is not through the name because that will be ridiculous. You need to import ClassVar from the module typing, and you need to wrap the type of the class attribute.

02:16 Those need to be wrapped in ClassVar. So ClassVar is what’s responsible for differentiating a class_attribute from an instance_ attribute, and then instance attributes and attributes in general might even get default values, for example. Now after your regular methods, you can also have static methods and class methods.

02:37 And for that, you just need to use the appropriate decorator. So to specify a class method member, you use the @classmethod decorator and you might also insert your type hints here.

02:52 And for a static method, you do a similar thing, you use the @staticmethod decorator

02:58 and maybe this has no arguments and returns an integer, for example. So this allows you to define members that are regular methods, class methods, or static methods.

03:09 You can also have properties as mentioned before. And for that, you use the decorator @property

03:16 and you specify the property name and its type. For example, this might be a Boolean, and you can also have setters and or deleters. You just have to define them as you would with a regular property.

03:30 For example, property_name.setter.

03:36 This would allow you to set your new property.

03:42 And finally, to create abstract methods, you need the @abstractmethod decorator. So first you need to import it, so jump to the beginning of the file,

03:54 and import abstractmethod from the module abc.

04:00 And now if you jump to the end of your protocol again,

04:06 you can now use the @abstractmethod decorator around a method that you want to make abstract. And what this will do is tell static type checkers that classes that implement your ProtocolMembersDemo must also have this abstract method.

04:27 As you can see, you can define plenty of different members for your protocols and what you do define will obviously depend on your situation. It’s just a matter of you knowing that you have this flexibility that allows you to create protocols that are more specialized than just protocols with simple instance methods.

04:49 That’s it for this lesson. In the next lesson, you will learn how to create subprotocols.

Become a Member to join the conversation.