Hướng dẫn why is python weakly typed? - tại sao python được gõ yếu?
There are some important issues that I think all of the existing answers have missed. Show 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:
On a little-endian platform with 32-bit integers, this makes
And of course this means I can overwrite memory anywhere in the system.*
* 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 * You can use a function like 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 1, and you pass it in 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 ( 3 gives you 4), while Python does a lot fewer ( 3 raises an exception, but 6 does give you 7*). It's just hard to put that sense into formal terms—why shouldn't there be a
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 9 is true. I don't think there's any sense in which 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 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 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. Python có bị đánh máy yếu không?Python được gõ mạnh mẽ gõ mạnh có nghĩa là loại đối tượng không thay đổi theo những cách bất ngờ.Một chuỗi chỉ chứa các chữ số không trở thành một số kỳ diệu, vì có thể xảy ra trong các ngôn ngữ được đánh máy yếu như JavaScript và Perl.Mọi thay đổi của loại đều yêu cầu chuyển đổi loại rõ ràng (còn gọi là đúc).
Strong typing means that the type of an object doesn't change in unexpected ways. A string containing only digits doesn't magically become a number, as may happen in weakly typed languages like JavaScript and Perl. Every change of type requires an explicit type conversion (aka casting).
Tại sao Python bị gõ mạnh?Python được gõ mạnh vì nó không có lỗi loại động không được kiểm soát.Nói cách khác, bạn không thể vi phạm hệ thống loại của nó.
Tại sao Python là một ngôn ngữ được đánh máy động?Python không có bất kỳ vấn đề nào ngay cả khi chúng tôi không khai báo loại biến.Nó nói rằng loại biến trong thời gian chạy của chương trình.Python cũng quan tâm đến việc quản lý bộ nhớ rất quan trọng trong lập trình.Vì vậy, Python là một ngôn ngữ được đánh máy động.Python also take cares of the memory management which is crucial in programming. So, Python is a dynamically typed language.
Tại sao các ngôn ngữ gõ yếu tồn tại?Các ngôn ngữ loại yếu vẫn đang được phát triển vì mọi người sử dụng chúng và thích chúng.because people use them and like them. |