Python for the .NET developer Transcripts
Chapter: OOP: Object-Oriented Python
Lecture: Object-Oriented Python
0:00 Let's talk about object oriented Python
0:03 writing classes, inheritance, creating objects
0:06 modeling things like, well, you are probably pretty
0:08 used to from C#.
0:10 It turns out that Python has excellent class inheritance
0:14 object oriented support.
0:16 So, let's talk about some of the features that Python has.
0:20 Everything is an object. Now, you can sort of say this
0:22 also for .NET or for C#, everything is an object there
0:26 as well. Everything derives or can derive from System.Object
0:30 That's almost true for C#, because you have value types
0:33 and reference types in order to treat the value types
0:36 as an object, you have to box them and unbox them and
0:39 there's some complications in there.
0:41 In Python, truly everything is a reference type.
0:44 Even the numbers are reference types.
0:46 And all of those reference types derive
0:48 from the object class.
0:52 We have instance methods and we have static, also something
0:55 called class methods. These kind of behave similarly.
0:58 just like we have in C#, you can create an
1:00 instance of an object and it has its behaviors or the type
1:03 has, like, static behaviors, got that, properties.
1:07 Remember the days when you used to write get value and
1:09 set value because you needed validation or these values
1:12 were computed, or something like that.
1:14 And C# added properties, which is great.
1:17 Python also has really good support for properties.
1:21 If you want to hide data, that is, like, private data
1:25 within your class, Python doesn't have the keywords around
1:29 public, private, internal, protected, those kinds of things.
1:33 But, there are several levels of mechanisms in the language
1:37 to have private data within your classes.
1:40 We also have inheritance. In fact, Python has multiple
1:43 inheritance which is usually, actually doesn't even appear.
1:46 Sometimes it shows up, sometimes it gets used, but it's
1:50 actually quite rare that multiple inheritance aspect of
1:53 Python's OOP shows up. But there's a rich inheritance
1:57 structure, like you have in C#.
1:59 We can overload operators and we can overload methods
2:03 much like you can change what equals means, or what
2:07 hash means, or double equals or divide in C#.
2:10 We can do the same thing with our Python classes.
2:14 We can implement special interfaces.
2:17 Either this can be deriving from a class and doing
2:19 something like that.
2:20 Or, there's a whole host of these special methods
2:23 that are like IDisposable.
2:25 Remember we talked about with and the compared to
2:28 the using block.
2:29 There's a set of functions you implement, you effectively
2:33 implement the usage within that with block.
2:35 It's not technically an interface, while it's in
2:37 quotes here, but the outcome is the same.
2:41 You also have abstract methods and abstract classes.
2:44 If you want to create a base class, you can't create
2:46 an instance of, but you can use it as a base class
2:49 totally supported in Python.
2:51 So, you can see, there's a lot here.
2:52 This is not just some little bolt-on thing or it's
2:55 not that it doesn't exist.
2:56 OOP in Python is proper object oriented programming.
3:00 We're going to go build some really cool classes and
3:02 model a particular environment and put them into action.