像 python 这样的动态语言可以给你带来什么?来自 ac#/java 背景。让我看看光!

发布于 2024-09-07 16:57:54 字数 321 浏览 3 评论 0原文

可能的重复:
对动态语言的热爱是怎么回事

我来自 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

空城旧梦 2024-09-14 16:58:05

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.

浴红衣 2024-09-14 16:58:03

本身不能代表 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.

最美的太阳 2024-09-14 16:58:02

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.

埖埖迣鎅 2024-09-14 16:58:00

我喜欢阅读这篇 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.

江挽川 2024-09-14 16:57:58

我不认为动态类型语言“允许很酷的技巧”(它们确实如此,但大多数情况下,在生产软件中使用“酷”技巧并不是真正合理——它们在测试、调试等方面派上用场,但是当它涉及到为生产部署好的、快速的东西,简单性规则)。

相反,我认为此类语言“不会妨碍我”——特别是,不会强迫我一遍又一遍地重复指定事物,从而减慢我的速度。并非所有静态类型语言都会“妨碍你”——像 Haskell 这样具有可靠、逻辑正确的类型系统的优秀语言可以让编译器推断出类型(尽管你可能会多余地指定)如果您喜欢冗余...或者更重要的是,如果您想要比编译器实际从代码中推断出的约束更严格的约束)。但在 Java 中(在 C# 中,除非您使用相当新的 var 关键字),冗余是规则,这会影响生产力。

Python 的第三方检查系统可以提供折衷方案,例如 typecheck - 我不这样做我自己不使用它,但我可以看到那些真正认为静态类型检查增加了很多价值的人可能会对此感到满意。最近的 Python 版本中甚至有一种语法(Python 编译器接受但不执行任何操作)可以让您注释函数参数和返回值 - 其目的是让诸如 typecheck 这样的包扩展到与适当的语言更自然地合并(尽管我认为 typecheck 还没有做到)。

编辑

正如我在这里所写的,我引用:

我喜欢范罗伊的解释和
哈里迪,p。 他们的书的 104-106,
尽管我可能同意也可能不同意
他们的结论(基本上是
本质差异很小
——他们指出 Oz 和 Alice 是可互操作的语言,无需 和
分别使用静态类型),所有
他们提出的观点很好。最多
重要的是,我相信,方法
动态类型允许真正的模块化
(静态类型更难,因为类型
必须在整个范围内执行纪律
模块边界)和“探索性
在计算模型中进行计算
集成了多种编程
范式”。

“建议使用动态类型”,他们
结论是,“当程序必须是
尽可能灵活”。我建议
阅读敏捷宣言
了解为什么最大灵活性是
在大多数实际应用中至关重要
编程——因此为什么,在
说的是现实世界而不是现实世界
更多学术界 Van Roy 博士和
Hadidi 博士入住,动态打字
通常更可取,而不是这样
小问题,因为它们有所作为
成为。尽管如此,他们至少展示了更多
对问题的认识,致力于 3
精彩的讨论页面
它的优点和缺点,比几乎任何
我看过的其他书——大多数书
已明确界定并执行
以一种或另一种方式优先,所以
讨论很少如此平衡
就这样;)。<​​/p>

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 think typecheck does yet).

Edit:

As I wrote here, and I quote:

I love the explanations of Van Roy and
Haridi, p. 104-106 of their book,
though I may or may not agree with
their conclusions (which are basically
that the intrinsic difference is tiny
-- they point to Oz and Alice as interoperable languages without and
with static typing, respectively), all
the points they make are good. Most
importantly, I believe, the way
dynamic typing allows real modularity
(harder with static typing, since type
discipline must be enforced across
module boundaries), and "exploratory
computing in a computation model that
integrates several programming
paradigms".

"Dynamic typing is recommended", they
conclude, "when programs must be as
flexible as possible". I recommend
reading the Agile Manifesto to
understand why maximal flexibility is
crucial in most real-world application
programming -- and therefore why, in
said real world rather than in the
more academic circles Dr. Van Roy and
Dr. Hadidi move in, dynamic typing is
generally preferable, and not such a
tiny issue as they make the difference
to be. Still, they at least show more
awareness of the issues, in devoting 3
excellent pages of discussion about
it, pros and cons, than almost any
other book I've seen -- most books
have clearly delineated and preformed
precedence one way or the other, so
the discussion is rarely as balanced
as that;).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文