简体   繁体   English

Python如何区分不同的数据类型?

[英]How does Python differentiate between the different data types?

Sorry if this is quite noobish to you, but I'm just starting out to learn Python after learning C++ & Java, and I am wondering how in the world I could just declare variables like id = 0 and name = 'John' without any int 's or string 's in front! 很抱歉,如果这对您来说非常不合适,但我刚开始学习C ++和Java后学习Python,我想知道在世界上我怎么能声明变量如id = 0name = 'John'而没有任何前面的int或者string I figured out that perhaps it's because there are no ' 's in a number, but how would Python figure that out in something like def increase(first, second) instead of something like int increase(int first, int second) in C++?! 我想通了,也许是因为没有'的一个数字,但如何将Python的数字说出来的东西像def increase(first, second) ,而不是像int increase(int first, int second)在C ++? !

The literal objects you mention carry (pointers to;-) their own types with them of course, so when a name's bound to that object the problem of type doesn't arise -- the object always has a type, the name doesn't -- just delegates that to the object it's bound to. 您提到的文字对象当然带有( - )指向它们自己的类型,所以当一个名称绑定到该对象时,类型的问题不会出现 - 对象总是有一个类型,名称不会 - 只需将它委托给它所绑定的对象。

There's no "figuring out" in def increase(first, second): -- name increase gets bound to a function object, names first and second are recorded as parameters-names and will get bound (quite possibly to objects of different types at various points) as increase gets called. def increase(first, second):没有“搞清楚” def increase(first, second): - 名称increase被绑定到一个函数对象,名字firstsecond被记录为参数名称并且将被绑定(很可能是各种不同类型的对象)点数)因为increase被调用。

So say the body is return first + second -- a call to increase('foo', 'bar') will then happily return 'foobar' (delegating the addition to the objects, which in this case are strings), and maybe later a call to increase(23, 45) will just as happily return 68 -- again by delegating the addition to the objects bound to those names at the point of call, which in this case are ints. 所以说身体首先return first + second - increase('foo', 'bar')调用increase('foo', 'bar')然后会愉快地返回'foobar' (将添加委托给对象,在这种情况下是字符串),也许以后increase(23, 45)的调用将同样愉快地返回68 - 再次通过将加法委托给在调用点处绑定到这些名称的对象,在这种情况下是整数。 And if you call with incompatible types you'll get an exception as the delegated addition operation can't make sense of the situation -- no big deal! 如果你使用不兼容的类型调用,你会得到一个例外,因为委托的加法操作无法理解这种情况 - 没什么大不了的!

Python is dynamically typed : all variables can refer to an object of any type. Python是动态类型的 :所有变量都可以引用任何类型的对象。 id and name can be anything, but the actual objects are of types like int and str . idname可以是任何东西,但实际的对象是intstr之类的类型。 0 is a literal that is parsed to make an int object, and 'John' a literal that makes a str object. 0是解析为生成int对象的文字, 'John'是生成str对象的文字。 Many object types do not have literals and are returned by a callable (like frozenset —there's no way to make a literal frozenset, you must call frozenset .) 许多对象类型没有文字并且由可调用的frozenset返回(比如frozenset - 没有办法制作文字的冻结集,你必须调用frozenset 。)

Consequently, there is no such thing as declaration of variables, since you aren't defining anything about the variable. 因此,没有变量声明这样的东西,因为你没有定义关于变量的任何东西。 id = 0 and name = 'John' are just assignment. id = 0name = 'John'只是赋值。

increase returns an int because that's what you return in it; increase返回一个int因为这是你返回的内容; nothing in Python forces it not to be any other object. Python中没有任何东西强迫它不是任何其他对象。 first and second are only ints if you make them so. 如果你这样做的话, firstsecond只是整数。

Objects, to a certain extent, share a common interface. 对象在一定程度上共享一个公共接口。 You can use the same operators and functions on them all, and if they support that particular operation, it works. 您可以在它们上面使用相同的运算符和函数,如果它们支持该特定操作,则它可以工作。 It is a common, recommended technique to use different types that behave similarly interchangably; 这是一种常见的推荐技术,可以使用不同类型,可以互换的方式相似; this is called duck typing . 这叫做鸭子打字 For example, if something takes a file object you can instead pass a cStringIO.StringIO object, which supports the same method as a file (like read and write ) but is a completely different type. 例如,如果某些东西采用file对象,则可以改为传递cStringIO.StringIO对象,该对象支持与文件相同的方法(如readwrite ),但是它是完全不同的类型。 This is sort of like Java interfaces, but does not require any formal usage, you just define the appropriate methods. 这有点像Java接口,但不需要任何正式用法,只需定义适当的方法即可。

Python uses the duck-typing method - if it walks, looks and quacks like a duck, then it's a duck. Python使用鸭子打字方法 - 如果它像鸭子一样走路,看起来和嘎嘎叫,那么它就是一只鸭子。 If you pass in a string, and try to do something numerical on it, then it will fail. 如果传入一个字符串,并尝试对其进行数字处理,那么它将失败。

Have a look at: http://en.wikipedia.org/wiki/Python_%28programming_language%29#Typing and http://en.wikipedia.org/wiki/Duck_typing 看看: http//en.wikipedia.org/wiki/Python_%28programming_language%29#Typinghttp://en.wikipedia.org/wiki/Duck_typing

When it comes to assigning literal values to variables, the type of the literal value can be inferred at the time of lexical analysis. 在将字面值分配给变量时,可以在词法分析时推断出文字值的类型。 For example, anything matching the regular expression (-)?[1-9][0-9]* can be inferred to be an integer literal. 例如,任何与正则表达式(-)?[1-9][0-9]*匹配的内容都可以推断为整数文字。 If you want to convert it to a float, there needs to be an explicit cast. 如果要将其转换为浮点数,则需要进行显式转换。 Similarly, a string literal is any sequence of characters enclosed in single or double quotes. 类似地,字符串文字是用单引号或双引号括起来的任何字符序列。

In a method call, the parameters are not type-checked. 在方法调用中,参数未经过类型检查。 You only need to pass in the correct number of them to be able to call the method. 您只需传入正确数量的它们即可调用该方法。 So long as the body of the method does not cause any errors with respect to the arguments, you can call the same method with lots of different types of arguments. 只要方法的主体不会导致参数的任何错误,您就可以使用许多不同类型的参数调用相同的方法。

In Python, Unlike in C++ and Java, numbers and strings are both objects. 在Python中,与C ++和Java不同,数字和字符串都是对象。 So this: 所以这:

   id = 0
   name = 'John'

is equivalent to: 相当于:

   id = int(0)
   name = str('John')

Since variables id and name are references that may address any Python object, they don't need to be declared with a particular type. 由于变量idname是可以寻址任何Python对象的引用,因此不需要使用特定类型声明它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM