Before C# I worked with C++ and before that I worked with VB6, I've also toyed with PHP and F# in the past but can't claim to have used them in any kind of detail. The one big lesson that I've learnt from these forays into other languages is that another language's perspective on problem solving can be so radically different to your current language of choice as to teach you useful lessons about programming in general.
With this in mind and fancying something a bit new, I thought it'd be cool to take a look at developing with Python. I wasn't wrong, Python is cool! I'll dive straight into my first impressions with a code comparison.
C#
/* This is a multi-line comment */ // This is a single line comment public class Example : BaseClass { private readonly string _name = "unknown"; // Constructor public Example(string name) { _name = name; } // Example method public string ExampleMethod() { var result = "hello "; resltu = result + _name; // Intentional typo return result; } }
Python
""" This is a multi-line comment """ # This is a single line comment class Example(BaseClass): __name = 'unknown' # Constructor def __init__(self, name): self.__name = name # Example method def exampleMethod(self): result = 'hello ' resltu = result + __name # Intentional typo return result;
There are some familiar elements here like classes, variables, methods, assigning values, returning values, constructors, comments, etc. However, the first thing that a C# programmer will likely notice when looking at the Python example is that there are no curly braces, so how does Python define scope?
Python defines scope by the indentation and because of this, indentation is non-optional. If you indent the class members incorrectly then you'll get an Indentation Error. I like this feature! We're all indenting our code anyway and fewer lines spent on managing scope means more lines of code on screen at any one time. Looking at the two samples, Python looks much cleaner to me.
Python also feels very quick to get up and running and working with the command line and text editor combo gives a nostalgic feeling of being closer to the code, though granted you can work this way with any language.
It's not all praise for Python, though. My first concern can be seen in the samples above. The C# sample won't compile, because resltu is a variable that hasn't been defined. Visual Studio will point you to the line and say "Did you mean to do this?" You can then declare the variable or correct your mistake. To Python, what we've said here is create a variable named result if it doesn't exist and assign "hello ". Now create another variable named resltu and assign it the value of result and our field __name. Now return the original variable result. In the sample, this typo is a bug, but Python will accept it.
My second concern so far is that there is no direct equivalent to C#'s access modifiers in Python. In the C# sample above, consumers of the class Example cannot access _name because it is marked as private. In fact, we can't modify it after construction either, because it's readonly, too. In Python, however, the most we can do is indicate to consumers of the class that the field __name is private by preceding it with a double underscore as we have done. Consumers of the class can still access it if they want via _Example__name.
As a developer in the C# headspace, this is alarming to me. The response from the Python community is that "we're all consenting adults here", but anybody who's worked on an Enterprise level application will know that not all programmers in a large team are of the same skill level and that this feature opens the door for potential, ill-considered misuse.
Over the coming months I will no doubt uncover a lot of Python's interesting gems and, when I do, I'll be blogging about those and their C# equivalents as well. If you happen to know of useful features available in Python that don't exist in .NET then I'd love to hear about them in the comments below. I'd also be really interested to hear how real my concerns are. If you've been working with Python at an Enterprise scale, do you run into these issues very often or is everyone better behaved because the reins are loosened?
No comments:
Post a Comment