Python for the .NET Developer Transcripts
Chapter: The Python Language
Lecture: C# type definitions

Login or purchase this course to watch this video and the rest of the course contents.
0:00 This is a little section that you are going to be very excited about as a C# developer. It's probably been a little funky for you to deal
0:09 with Python in its dynamic language types and sometimes you're just like, oh, what is this? What operations can I do on this element
0:16 or why can't I say this is an integer or something like that. We're going to look at the C# type system
0:22 super quick because I'm sure you know it really well and then we're going to do something similar in Python, surprisingly.
0:28 So we've got a class, it is a Wizard, like from a game or something and I know normally you would put this
0:33 in its own wizard.cs file but with all this stuff going on we're like running low on grouping options so I'm putting it in this typing file here, okay?
0:41 It has a string, which is a name it has a name which is a string, rather it's a level, which is an integer and it has this factory method called Train.
0:50 You can pass in a base level and out pops a wizard that is trained up to that level. Not a lot going on there, but whatever.
0:58 And then here's the main method. We're going to create a wizard, we're going to call him Gandalf
1:02 and we're going to level him up a little bit and we're going to say the level of the wizard is whatever their level happens to be, okay? Run this.
1:11 Awesome, the level of the wizard is 8, and it started out as 7 and obviously it compiles and runs. So this is just a real simple case
1:19 of working with types, right? We have a wizard class, we say dot, we get Train. Obviously if we can explicitly state this is a wizard
1:30 and everything works, but if we tried to say it's an int obviously not so much, right? Not so happy.
1:37 If we try to change this, obviously you can't just leave it void or whatever, right? We have to say either var, where it implicitly
1:44 adapts the type that is returned here explicitly a wizard, or we have to say it's a wizard like so, right?
1:50 And once we do that we have all the type options, name and level and things like that that are a part of this class.
1:58 That's C# typing, and let's just run it one more time to make sure I didn't break it, 'cause it compiled while I was fiddling with it.
2:04 Great, Gandalf is level 8 in C#.


Talk Python's Mastodon Michael Kennedy's Mastodon