强类型弱类型与动态类型
时间:2020-06-20 04:47:46 来源:望德堂环保厂家 浏览量:1
Summary
With the advent of languages such as Python, the debate over typing has heated up again. Contrary to some claims (notably from Bruce Eckel), I believe Python has strong typing, and this article explains why.
What is a "type", anyway? Before talking about what kind of type system a language supports, we should establish agreement about what a type is in the first place. My definition is that a type is metadata about a chunk of memory that classifies the kind of data stored there. This classification usually implicitly specifies what kinds of operations may be performed on the data. Common types include primitive types (strings and numbers), container types (lists/arrays and dictionaries/hashes), and user-defined types (classes). In Python, everything is an object, and every object has a type. In other words, functions, modules, and stack frames are also types. So what's "strong typing", then? From my POV, strong typing prevents mixing operations between mismatched types. In order to mix types, you must use an explicit conversion. Here's a simple Python example: 1 + "1"Traceback (most recent call last): File "", line 1, in ?TypeError: unsupported operand type(s) for +: 'int' and 'str' 1 + 12 "1" + "1"'11' 1 + int("1")2 "1" + str(1)'11'
Conversely, weak typing means that you can mix types without an explicit conversion. Consider this example from Perl: DB1 print "1"+12 DB2 print "1".111
Note that conversion is not the same thing as coercion, IMO. Coercion occurs when you have a statically-typed language and you use the syntactic features of the language to force the usage of one type as if it were a different type (consider the common use of void* in C). Coercion is usually a symptom of weak typing. Conversion, OTOH, creates a brand-new object of the appropriate type. Why do some people think Python has weak typing? Historically, "strong typing" has been associated with static typing. Languages noted for strong typing include Pascal and Ada; languages noted for weak typing (most notoriously BASIC) had primarily dynamic typing. But the language that ought to be most notorious for weak typing has static typing: C/C++ (yes, I'm lumping them together) It's very clear that Python has only dynamic typing; any target may hold a binding to any kind of object. More than that, Pythonic programming style is to use inheritance primarily for implementation; Python's name-based polymorphism means that you rarely need to inherit for interface. In fact, the primary exception to inheriting for implementation is Python exceptions, which uses issubclass() for the purpose of determining which exceptions get caught by an except clause. I might even go so far as to say that Python's name-based polymorphism is hyperpolymorphic. And therein lies the tiny kernel of truth about Python's weak typing. People who have gotten used to Java and C++ requiring syntactic support to declare typing often feel uncomfortable with the Pythonic style of relying on run-time exceptions to get thrown when an inappropriate object is passed around: class Silly: def __init__(self, data): ta = data def __add__(self, other): return str(ta) + str(ta)def double(a): return a + aprint double(1)print double('x')print double([1])print double(Silly({'a':1}))print double({'a':1})
produces
2xx[1, 1]{'a': 1}{'a': 1}Traceback (most recent call last): File "", line 14, in ? print double({'a':1}) File "", line 8对方一个眼神的功力就让蔡依林完全无法招架, in double return a + aTypeError: unsupported operand types for +: 'dict' and 'dict'
Bruce Eckel equates "weak typing" with "latent typing", but that's at odds with historical usage, not to mention that it confuses the two axes of strong/weak and static/dynamic. Sidebar: Name-based polymorphism For those of you unfamiliar with Python, here's a quick intro to name-based polymorphism. Python objects have an internal dictionary that contains a string for every attribute and method. When you access an attribute or method in Python code, Python simply looks up the string in the dict. Therefore, if what you want is a class that works like a file标准化却是没商量的相似。弯曲的小趾在标准的鞋里受到挤压, you don't need to inherit from file, you just create a class that has the file methods that are needed. Python also defines a bunch of special methods that get called by the appropriate syntax. For example, a+b is equivalent to a.__add__(b). There are a few places in Python's internals where it directly manipulates built-in objects, but name-based polymorphism works as you expect about 98% of the time. Resources Discussions of types TUNES : Type System
ttp://type from FOLDOC
ttp://Python Java: Side by Side Comparison
ttp:// Bruce Eckel and Python's weak typing Artima Interview: Type Checking and Techie Control
ttp://Strong Typing vs. Strong Testing
ttp:// Talk Back!Have an opinion? Be the first to post a comment about this weblog entry. RSS FeedIf you'd like to be notified whenever Aahz adds a new entry to his weblog, subscribe to his RSS feed. About the BloggerAahz has been using Python since 1999. He helps people on thon, and is one of the webmasters for . Aahz focuses on the Python object model and Python threads. Aahz is currently working on "Effective Python" for Addison Wesley.This weblog entry is Copyright 2003 Aahz. All rights reserved.
下肢静脉血栓的治疗感冒鼻窦炎症状大同治疗白斑病费用
- 上一篇:泰国潜水员扮成圣诞老人水中喂鱼图
- 下一篇:国产数控系统助力企业机床改造
-
循循善诱训练博美犬嗅闻位置
循循善诱 训练博美犬嗅闻循循善诱 训练博美犬嗅闻 16:11:59出处:络点击:1666 导读: 目的:嗅源是鉴别的唯一依...[详细]
2022-06-03
-
彼得秃猫和斯芬克斯有什么区别位置
彼得秃猫和斯芬克斯有什么区别 22:53 来源:络 作者:络 彼得秃猫和斯芬克斯有什么区别...[详细]
2022-06-03
-
影响鸟便便颜色的因素有哪些位置
影响鸟便便颜色的因素有哪些影响鸟便便颜色的因素有哪些 15:35:53by 王慧文我们平时见得最多的鸟粪的颜色一般为白色,但是鸟粪的颜色绝对不仅仅局限...[详细]
2022-06-03
-
防疫知识藏獒淘气咬伤人应该怎么办位置
前天下午驱虫的,后来就吐了,食欲感觉不是很好,昨天早上为他平时的饭量,他没有吃完,这样的现象正常么?浪漫情人:藏獒吃完驱虫药后 会刺激肠...[详细]
2022-06-03
-
防治阿拉斯加犬的犬窝咳疾病有哪些建议位置
每当到了季节变化的时候,都是阿拉斯加犬的传染性疾病复发最频繁的时候了。这个时候的阿拉斯加犬身体素质也是比较差的时候,所以会更加容易患上...[详细]
2022-06-03
-
防治罕见皮肤病茶杯犬的黑色棘皮症应当怎样位置
在饲养茶杯犬的过程中,主人们常常会发现茶杯犬会遇到很多种的皮肤病。这些皮肤病的种类是不一样的,严重的程度与治疗的方法也是有一定的区别的...[详细]
2022-06-03