4 ms·
Well in JS you could do String.prototype.fnName = function(){}; to add fnName as a method to all strings even if they are already instantiated, is that possible
by emehrkay 15y ago
Well in JS you could do String.prototype.fnName = function(){}; to add fnName as a method to all strings even if they are already instantiated, is that possible with Python?
- oinksoft 15y agoOf course. http://codepad.org/3fWweqje http://codepad.org/3fWweqje
- deleted 15y ago[deleted]
- emehrkay 15y agoCan it be done for built-in types?
- maaku 15y agoNo, unfortunately. That's one thing both Ruby and JS have over Python.
- oinksoft 15y agoNo, it can't, at least not in CPython. You can import __builtin__ and clobber str, for instance, but that will not affect the behavior of existing strings, nor will it influence the behavior of string literals. It's something that can be done in Ruby and JavaScript, but I believe it was never allowed in Python in the name of performance (you incur overhead when every number is Fixnum with the same lookup rules as any other instance, for example), and also safety.
- dorkitude 15y agoNo. In Python, primitives prototypes aren't mutable. Pythonic code is meant to be modular, which leads to extreme explicitness. For example, while `string_utils.capitalize(my_string)` is a bit more longwinded than `my_string.capitalize()` in JS or even `my_string.capitalize` in Ruby, this minor bit of ongoing typing overhead will yield a beautifully explicit codebase over time. By comparison to other dynamic languages like Javascript and Ruby, Python codebases scale effortlessly and beautifully and without the limitations of "How much freaking online documentation do I need to read to locate the imperative code that's actually running?". While that sort of necessity is the mother of beautiful documentation design you'll find in (and the ascendance of great doc writers among) the Ruby and Javascript communities, I believe its technical risks outweight those tertiary benefits. Extensive and explicit documentation is a necessity that Python does away with entirely, because the code is itself explicit. Python does this at the cost of some convenience and developer happiness. Whereas I give Ruby a 10/10 in the "day-to-day comfort" scale for us dynamic developers, and where Javascript has perhaps a 7/10, I place Python squarely at 9. By comparison to static high-level languages like Java and ActionScript (and some dynamic languages like Perl and PHP) classical, Python is of course a breeze to write. As a result of this explicitness, when using a Python framework, you'll rarely need consult documentation or outside advice just to find out what's happening: It's all in the code, and you just have to follow the imports and method calls down the stack. This modularity also means Pythonists totally avoid collisions in namespacing: whereas the global namespaces of Ruby and Javascript tend to lead to headaches in large codebases, Python is built on the idea of package-as-namespace (directories) and module-as-namespace (.py files)
- emehrkay 15y agoGreat reply, thanks.