Strong type, weak type and dynamic type

zhaozj2021-02-08  404

Unlike some rumors, Python is a strong type of language, but it is a dynamic type. Strong type, dynamic type, weak type, this is a few different (and often confusing) concepts.

----------------

Typing: Strong vs. weak, static vs. Dynamic

By aahz

July 15, 2003

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 type", THEN?

From My Pov, Strong Typing Prevents Mixing Operations Between Mismatch 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 1

2

>>> "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: DB <1> Print "1" 1

2

DB <2> PRINT "1" .1

11

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 Usess 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 Inappriate Object IS Passed Around: Class Silly:

DEF __INIT __ (Self, DATA):

Self.Data = DATA

DEF __ADD __ (Self, Other):

Return Str (Self.Data) Str (Other.Data)

Def Double (a):

Return A A

Print Double (1)

Print Double ('x')

Print Double ([1])

Print Double (Silly ({'A': 1}))

Print Double ({'a': 1})

Produces

2

xx

[1, 1]

{'a': 1} {'a': 1}

TRACEBACK (MOST RECENT CALL LAST):

File "Test.py", LINE 14, IN?

Print Double ({'a': 1})

File "Test.PY", LINE 8, in Double

Return A A

TypeError: unsupported Operand Types for : 'Dict' and 'DICT'

Bruce Eckel Equates "WEAK TYPING" with "latent Typing", Buthat's at Odds with Historical Usage, Not to Ment That It Confuses The Two Axes of Strong / Weak and Static / Dynamic.

Sidebar: name-based Polymorphismm

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. there is a class thing works like one, you don't need to inherit from file, you just create a class tria has the file methods.

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 expert About 98% of the time.resources

Discussions of Types

Tunes: type system http://cliki.tunes.org/type system

Type from foldoc http://wombat.doc.ic.ac.uk/foldoc/foldoc.cgi?type

Python & Java: Side by Side Comparison http://www.ferg.org/projects/python_java_side-by-side.html

Bruce Eckel and Python's Weak Typing

Artima Interview: Type Checking and Techie Control http://www.Artima.com/intv/typing.html

Strong typing vs. strong Testing http://mindview.net/weblog/log-0025

Talk back!

HAVE An Opinion? Be The First to Post A Comment About this Weblog Entry.

RSS feed

If You'D Like to Be Notified WHENEVER AAHZ Adds a New Entry to His Weblog, Subscribe to His RSS feed.

About the blogger

Aahz has been using Python since 1999. He helps people on comp.lang.python, and is one of the webmasters for www.python.org. 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.

转载请注明原文地址:https://www.9cbs.com/read-688.html

New Post(0)