Hướng dẫn python is a loosely typed programming language - python là một ngôn ngữ lập trình được đánh máy lỏng lẻo

There are some important issues that I think all of the existing answers have missed.


Weak typing means allowing access to the underlying representation. In C, I can create a pointer to characters, then tell the compiler I want to use it as a pointer to integers:

char sz[] = "abcdefg";
int *i = (int *)sz;

On a little-endian platform with 32-bit integers, this makes i into an array of the numbers 0x64636261 and 0x00676665. In fact, you can even cast pointers themselves to integers (of the appropriate size):

intptr_t i = (intptr_t)&sz;

And of course this means I can overwrite memory anywhere in the system.*

char *spam = (char *)0x12345678
spam[0] = 0;

* Of course modern OS's use virtual memory and page protection so I can only overwrite my own process's memory, but there's nothing about C itself that offers such protection, as anyone who ever coded on, say, Classic Mac OS or Win16 can tell you.

Traditional Lisp allowed similar kinds of hackery; on some platforms, double-word floats and cons cells were the same type, and you could just pass one to a function expecting the other and it would "work".

Most languages today aren't quite as weak as C and Lisp were, but many of them are still somewhat leaky. For example, any OO language that has an unchecked "downcast",* that's a type leak: you're essentially telling the compiler "I know I didn't give you enough information to know this is safe, but I'm pretty sure it is," when the whole point of a type system is that the compiler always has enough information to know what's safe.

* A checked downcast doesn't make the language's type system any weaker just because it moves the check to runtime. If it did, then subtype polymorphism (aka virtual or fully-dynamic function calls) would be the same violation of the type system, and I don't think anyone wants to say that.

Very few "scripting" languages are weak in this sense. Even in Perl or Tcl, you can't take a string and just interpret its bytes as an integer.* But it's worth noting that in CPython (and similarly for many other interpreters for many languages), if you're really persistent, you can use ctypes to load up libpython, cast an object's id to a POINTER(Py_Object), and force the type system to leak. Whether this makes the type system weak or not depends on your use cases—if you're trying to implement an in-language restricted execution sandbox to ensure security, you do have to deal with these kinds of escapes…

* You can use a function like

intptr_t i = (intptr_t)&sz;
0 to read the bytes and build a new int out of "how C would represent these bytes", but that's obviously not leaky; even Haskell allows that.


Meanwhile, implicit conversion is really a different thing from a weak or leaky type system.

Every language, even Haskell, has functions to, say, convert an integer to a string or a float. But some languages will do some of those conversions for you automatically—e.g., in C, if you call a function that wants a

intptr_t i = (intptr_t)&sz;
1, and you pass it in
intptr_t i = (intptr_t)&sz;
2, it gets converted for you. This can definitely lead to bugs with, e.g., unexpected overflows, but they're not the same kinds of bugs you get from a weak type system. And C isn't really being any weaker here; you can add an int and a float in Haskell, or even concatenate a float to a string, you just have to do it more explicitly.

And with dynamic languages, this is pretty murky. There's no such thing as "a function that wants a float" in Python or Perl. But there are overloaded functions that do different things with different types, and there's a strong intuitive sense that, e.g., adding a string to something else is "a function that wants a string". In that sense, Perl, Tcl, and JavaScript appear to do a lot of implicit conversions (

intptr_t i = (intptr_t)&sz;
3 gives you
intptr_t i = (intptr_t)&sz;
4), while Python does a lot fewer (
intptr_t i = (intptr_t)&sz;
3 raises an exception, but
intptr_t i = (intptr_t)&sz;
6 does give you
intptr_t i = (intptr_t)&sz;
7*). It's just hard to put that sense into formal terms—why shouldn't there be a
intptr_t i = (intptr_t)&sz;
8 that takes a string and an int, when there are obviously other functions, like indexing, that do?

* Actually, in modern Python, that can be explained in terms of OO subtyping, since

intptr_t i = (intptr_t)&sz;
9 is true. I don't think there's any sense in which
char *spam = (char *)0x12345678
spam[0] = 0;
0 is an instance of the string type in Perl or JavaScript… although in Tcl, it actually is, since everything is an instance of string.


Finally, there's another, completely orthogonal, definition of "strong" vs. "weak" typing, where "strong" means powerful/flexible/expressive.

For example, Haskell lets you define a type that's a number, a string, a list of this type, or a map from strings to this type, which is a perfectly way to represent anything that can be decoded from JSON. There's no way to define such a type in Java. But at least Java has parametric (generic) types, so you can write a function that takes a List of T and know that the elements are of type T; other languages, like early Java, forced you to use a List of Object and downcast. But at least Java lets you create new types with their own methods; C only lets you create structures. And BCPL didn't even have that. And so on down to assembly, where the only types are different bit lengths.

Vì vậy, theo nghĩa đó, hệ thống loại của Haskell mạnh hơn Java hiện đại, mạnh hơn Java trước đó, mạnh hơn C, mạnh hơn BCPL.

Vì vậy, Python phù hợp với quang phổ đó ở đâu? Đó là một chút khó khăn. Trong nhiều trường hợp, gõ vịt cho phép bạn mô phỏng mọi thứ bạn có thể làm trong Haskell và thậm chí một số thứ bạn không thể; Chắc chắn, các lỗi bị bắt trong thời gian chạy thay vì thời gian biên dịch, nhưng chúng vẫn bị bắt. Tuy nhiên, có những trường hợp gõ vịt không đủ. Ví dụ: trong Haskell, bạn có thể nói rằng một danh sách số trống của INTS là danh sách các INT, vì vậy bạn có thể quyết định rằng việc giảm

intptr_t i = (intptr_t)&sz;
8 trong danh sách đó sẽ trả về 0*; Trong Python, một danh sách trống là một danh sách trống; Không có thông tin loại nào để giúp bạn quyết định giảm
intptr_t i = (intptr_t)&sz;
8 so với nó nên làm.

* Trên thực tế, Haskell không cho phép bạn làm điều này; Nếu bạn gọi hàm giảm không lấy giá trị bắt đầu trong danh sách trống, bạn sẽ gặp lỗi. Nhưng hệ thống loại của nó đủ mạnh để bạn có thể thực hiện công việc này và Python thì không.

Tại sao Python được gọi là ngôn ngữ được gõ lỏng lẻo?

Một ngôn ngữ lập trình được gõ một cách lỏng lẻo, hoặc gõ yếu, khi nó không yêu cầu đặc tả rõ ràng của các loại đối tượng và biến khác nhau.Các quy tắc gõ "lỏng hơn" trong các ngôn ngữ lập trình được đánh máy yếu có thể tạo ra kết quả sai lầm hoặc không thể đoán trước.Nó có thể thực hiện các chuyển đổi loại ngầm trong thời gian chạy.when it does not require the explicit specification of different types of objects and variables. The "looser" typing rules in weakly typed programming languages can produce erroneous or unpredictable results. It can execute implicit type conversions at runtime.

Là một ngôn ngữ lập trình được đánh máy một cách lỏng lẻo?

Một ngôn ngữ được đánh máy một cách lỏng lẻo là một ngôn ngữ lập trình không yêu cầu một biến phải được xác định.Ví dụ: Perl là ngôn ngữ được gõ lỏng lẻo, bạn có thể khai báo một biến, nhưng nó không yêu cầu bạn phân loại loại biến.a programming language that does not require a variable to be defined. For example, Perl is a loosely typed language, you can declare a variable, but it doesn't require you to classify the type of variable.

Những ngôn ngữ nào được gõ lỏng lẻo?

Một ví dụ khác về một ví dụ ngôn ngữ được gõ lỏng lẻo là C ...
ActionScript..
Clojure..
Python..
Smalltalk..

Tại sao Python là một ngôn ngữ được đánh máy mạnh mẽ?

Python được gõ mạnh khi trình thông dịch theo dõi tất cả các loại biến.Nó cũng rất năng động vì nó hiếm khi sử dụng những gì nó biết để hạn chế sử dụng biến.the interpreter keeps track of all variables types. It's also very dynamic as it rarely uses what it knows to limit variable usage.