像 python 这样的动态语言可以给你带来什么?来自 ac#/java 背景。让我看看光!
可能的重复:
对动态语言的热爱是怎么回事
我来自 c# /java 背景即强类型、OOP 语言。
我对Python非常感兴趣,但我需要更多地了解动态语言的优点。
它到底给了我什么力量? (在网络应用程序中)。
有人可以概述一下我可以做的一些优势和很酷的技巧吗?
Possible Duplicate:
What’s with the love of dynamic Languages
I'm coming from a c#/java background i.e. strongly typed, OOP language.
I'm very much interested in Python, but I need to learn a little more about the advantages of a dynamic language.
What power does it really give me? (in web applications).
Can someone outline some of the advantages and cool tricks I can do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Python 是强类型和面向对象的,不同之处在于Python也是动态的。
在 Python 中,类是像其他所有对象一样的对象,并且与您可以在运行时创建和修改它们的所有其他对象一样。这基本上意味着您可以在运行时创建和更改模块、元类、类、属性/方法和函数。您可以将基类添加到已有的类和其他一些内容中。
Python is strongly typed and object oriented the difference is that Python is also dynamic.
In Python classes are objects like everything else and as every other object you can create and modify them at runtime. This basically means that you can create and change modules, metaclasses, classes, attributes/methods and functions at runtime. You can add base classes to already existing classes and several other things.
本身不能代表 python,但上周我在 Powershell 中使用 PSObject 类,它允许您动态添加成员、方法等。来自 C++\C# 背景,这看起来很神奇 - 不需要重新编译以内置这些构造,使其成为我正在做的事情的更好的工作流程。
Can't speak for python per se, but I was playing around with the PSObject class in Powershell last week which allows you to dynamically add members, methods, etc. Coming from a C++\C# background, this seemed like magic - no need to re-comile to get these constructs in-built making it a much nicer workflow for what I was doing.
Python(像所有动态语言一样)将属性查找推迟到运行时。这使您能够突破多态性和接口的思想,并利用 duck-typing 的强大功能,这样您就可以使用一种看起来应该可以工作的类型,而不必担心它的祖先或它声称要实现的内容。
Python (like all dynamic languages) defers attribute lookups until runtime. This allows you to break past the ideas of polymorphism and interfaces, and leverage the power of duck-typing, whereby you can use a type that merely looks like it should work, instead of having to worry about its ancestry or what it claims to implement.
我喜欢阅读这篇 Python 和 Java 之间的比较。
关于网络,我建议使用 Django 做一个简单的例子来看看它是如何工作的。
I enjoyed reading this comparison between Python and Java.
In relation with web, I would recommend doing a simple example with Django to see how it works.
我不认为动态类型语言“允许很酷的技巧”(它们确实如此,但大多数情况下,在生产软件中使用“酷”技巧并不是真正合理——它们在测试、调试等方面派上用场,但是当它涉及到为生产部署好的、快速的东西,简单性规则)。
相反,我认为此类语言“不会妨碍我”——特别是,不会强迫我一遍又一遍地重复指定事物,从而减慢我的速度。并非所有静态类型语言都会“妨碍你”——像 Haskell 这样具有可靠、逻辑正确的类型系统的优秀语言可以让编译器推断出类型(尽管你可能会多余地指定)如果您喜欢冗余...或者更重要的是,如果您想要比编译器实际从代码中推断出的约束更严格的约束)。但在 Java 中(在 C# 中,除非您使用相当新的
var
关键字),冗余是规则,这会影响生产力。Python 的第三方检查系统可以提供折衷方案,例如 typecheck - 我不这样做我自己不使用它,但我可以看到那些真正认为静态类型检查增加了很多价值的人可能会对此感到满意。最近的 Python 版本中甚至有一种语法(Python 编译器接受但不执行任何操作)可以让您注释函数参数和返回值 - 其目的是让诸如
typecheck
这样的包扩展到与适当的语言更自然地合并(尽管我认为typecheck
还没有做到)。编辑:
正如我在这里所写的,我引用:
I don't think of dynamically typed languages as "allowing cool tricks" (they do, but mostly it's not really sound to use "cool" tricks in production software -- they come in handy for testing, debugging, etc, but when it comes to getting good, fast stuff deployed for production, simplicity rules).
Rather, I think of such languages as "not getting in my way" -- in particular, not slowing me down by forcing me to redundantly specify things over and over. Not every statically typed languages does "get in your way" -- good ones with solid, logically correct type systems like Haskell let the compiler deduce types (though you may redundantly specify them if you like redundancy... or, more to the point, if you want stricter constraints than what the compiler can actually deduce from the code). But in Java (and to a lesser extent in C# except when you use the reasonably recent
var
keyword) redundancy is the rule, and that impacts productivity.A compromise can be offered by third-party checking systems for Python, like typecheck -- I don't use it, myself, but I can see how somebody who really thinks static type checking adds a lot of value might be happy with it. There's even a syntax (which the Python compiler accepts but does nothing with) in recent Python versions to let you annotate your function arguments and return values -- its purpose is to let such packages as
typecheck
be extended to merge more naturally with the language proper (though I don't thinktypecheck
does yet).Edit:
As I wrote here, and I quote: