Python变量在使用时绑定到类型? [英] Python variables are bound to types when used?
问题描述
所以我想定义一个带有boolean的方法。在一个模块中,例如。
def getDBName(l2):
....
现在,在Python变量使用时绑定到类型,对吧?
例如。
x = 10#使它成为INT
而
x =" hello" #使它成为一个字符串
我接受它,函数的参数(在上面的例子中为l2)是定义中绑定的
,而不是被调用。
所以,如果我使用l2因此:
如果(l2):#只有这样才能使它成为布尔值?
如果我这样做,
如果(l2 =" hello"):#它会变成字符串吗?
如果我从未在定义体中使用它会怎么样?
Elucidate请。
So I want to define a method that takes a "boolean" in a module, eg.
def getDBName(l2):
....
Now, in Python variables are bound to types when used, right?
Eg.
x = 10 # makes it an INT
whereas
x = "hello" # makes it a string
I take it, the parameters to a function (in the above example "l2") are
bound in the definition, rather than as invoked.
So, if I use "l2" thus:
if (l2): # only then does it make it a boolean?
and if I did,
if (l2 = "hello"): # would it become string?
and what if I never used it in the definition body?
Elucidate please.
推荐答案
pr *********** @ yahoo.com 写道:
pr***********@yahoo.com wrote:
所以我想要定义一个采用布尔的方法在一个模块中,例如,
def getDBName(l2):
...
现在,Python中的变量在使用时绑定到类型,对吗?
没有。变量绑定到对象,对象有类型。
例如。
x = 10#使它成为INT
no。它将名称x绑定在一起。到整数对象。
而
x =" hello" #使它成为一个字符串
no。 (重新)绑定名称x字符串对象。
我接受它,函数的参数(在上面的例子中为l2)在定义中绑定,而不是被调用。
不确定你在这说什么。当你调用一个函数时,每个参数
都绑定到相应参数所代表的对象。
所以,如果我使用l2因此:
如果(l2):#只有这样才能使它成为布尔值?
没有。查询对象以查看它是否为true。
如果我这样做,
if(l2 =" hello"):#would it成为字符串?
没有。这是一个语法错误;如果你解决了这个问题,它会查询对象以查看
如何将自己与给定的字符串对象进行比较。
Elucidate please。
So I want to define a method that takes a "boolean" in a module, eg.
def getDBName(l2):
...
Now, in Python variables are bound to types when used, right?
no. variables are bound to objects, and objects have types.
Eg.
x = 10 # makes it an INT
no. that binds the name "x" to an integer object.
whereas
x = "hello" # makes it a string
no. that (re)binds the name "x" to a string object.
I take it, the parameters to a function (in the above example "l2") are
bound in the definition, rather than as invoked.
not sure what you''re saying here. when you call a function, each parameter
is bound to the object represented by the corresponding argument.
So, if I use "l2" thus:
if (l2): # only then does it make it a boolean?
no. that queries the object to see if it''s "true".
and if I did,
if (l2 = "hello"): # would it become string?
no. that''s a syntax error; if you fix that, it queries the object to see
how compares itself to the given string object.
Elucidate please.
>
重置你的大脑:
http ://effbot.org/zone/python-objects.htm
< / F>
reset your brain:
http://effbot.org/zone/python-objects.htm
</F>
>
2005-10-19, pr *********** @ yahoo .com < pr *********** @ yahoo.com>写道:
On 2005-10-19, pr***********@yahoo.com <pr***********@yahoo.com> wrote:
所以我想定义一个采用布尔的方法。在一个模块中,例如,
def getDBName(l2):
...
现在,Python中的变量在使用时绑定到类型,对吗?
Python没有变量。
Python有各种类型的对象。你可以绑定0或者更多
命名一个对象。
例如。
x = 10#使它成为INT
而
x =你好 #使它成为一个字符串
不,不是真的。没有它那个'变得与众不同
类型。
x = 10
创建一个值为10的整数对象并绑定名称
" x"它是
x =" hello"
创建一个包含值hello的字符串对象。然后
取消绑定名称x从整数对象中重新绑定到
字符串对象。 [此时,整数对象_may_
会被删除,如果它不再被使用(它可能已经有多个名字的
)。]
我接受它,函数的参数(在上面的例子中为l2)在定义中绑定,而不是被调用。
不确定我理解这个问题。您定义的函数
接受单个对象作为参数。当调用函数
时,该对象具有本地名称l2。绑定它
所以,如果我使用l2因此:
如果(l2):#只有这样才能使它成为布尔值?
这不会影响名称为l2
的对象类型。它检查l2是否有假值。
具有错误值的基本对象的示例是迭代器0,
浮点数0.0,空字符串" ;",空列表[],
空元组()或空字典{}。
如果我这样做,
if(l2 =" hello"):#会变成字符串吗?
这不是合法的蟒蛇。我认为你的意思是
如果l2 ==" hello":
表达式
l2 ==" hello"
检查名称为l2的对象是否为是一个带有
值的字符串hello。如果您传递给函数的对象是一个字符串对象,其值为hello,那么该扩展将为
为真。
不是字符串的任何对象的表达式都是false,而对于没有
的任何字符串对象,该表达式的值为hello。
如果我从未在定义体中使用过它会怎么样?
So I want to define a method that takes a "boolean" in a module, eg.
def getDBName(l2):
...
Now, in Python variables are bound to types when used, right?
Python doesn''t have variables.
Python has objects of various types. You can bind 0 or more
names an object.
Eg.
x = 10 # makes it an INT
whereas
x = "hello" # makes it a string
No, not really. There is no "it" that''s becoming different
types.
x = 10
creates an integer object with the value 10 and binds the name
"x" to it.
x = "hello"
creates a string object containing the value "hello" and then
unbinds the name "x" from the integer object and re-binds it to
the string object. [At that point, the integer object _may_
get deleted if it''s not being used any longer (it may have had
multiple names).]
I take it, the parameters to a function (in the above example "l2") are
bound in the definition, rather than as invoked.
Not sure I understand the question. The function you defined
accepts a single object as a parameter. When the function is
invoked, that object has the local name "l2" bound to it
So, if I use "l2" thus:
if (l2): # only then does it make it a boolean?
That doesn''t affect the type of the object with the name "l2"
at all. It checks to see if l2 has a false value or not.
Examples of basic objects with false values are an iteger 0, a
floating point 0.0, an empty string "", an empty list [], an
empty tuple (), or an empty dictionary {}.
and if I did,
if (l2 = "hello"): # would it become string?
That''s not legal python. I presume you mean
if l2 == "hello":
The expression
l2 == "hello"
checks to see if the object with the name "l2" is a string with
the value "hello". If the object you passed to the function is
a string object with the value "hello", that experession will
be true. The expression will be false for any object that
isn''t a string, and false for any string object that doesn''t
have the value "hello".
and what if I never used it in the definition body?
然后它就不会被使用了。
-
格兰特爱德华兹格兰特哇!我忘记了我的
整个生活哲学!!!
visi.com
Then it doesn''t get used.
--
Grant Edwards grante Yow! I just forgot my
at whole philosophy of life!!!
visi.com
2005年10月19日12:51:02 -0700, pr *********** @ yahoo.com
< pr *********** @ yahoo.com>写道:
On 19 Oct 2005 12:51:02 -0700, pr***********@yahoo.com
<pr***********@yahoo.com> wrote:
所以我想定义一个采用boolean的方法。在一个模块中,例如,
def getDBName(l2):
...
现在,Python中的变量在使用时绑定到类型,对吗?
Python并没有真正的变量。它有对象,其中键入了
,而名字则没有。
例如。
x = 10#使它成为INT
名称''''现在绑定到一个int。
而
x =" hello" #使它成为一个字符串
现在它已经绑定了一个字符串。
我接受它,函数的参数(在上面的例子中) ; l2)在定义中绑定,而不是被调用。
所以,如果我使用l2,那么因此:
如果(l2):#那么它是否会使它成为布尔值?
如果我这样做,
if(l2 = hello):#它会变成字符串吗?
如果我从未在定义体中使用它会怎么样?
现在你失去了我。可能是我的问题 - 我可以从酒吧发布
吧。
Elucidate请。
So I want to define a method that takes a "boolean" in a module, eg.
def getDBName(l2):
...
Now, in Python variables are bound to types when used, right?
Python doesn''t really have variables as such. It has objects, which
are typed, and names, which are not.
Eg.
x = 10 # makes it an INT
The name ''x'' is now bound to an int.
whereas
x = "hello" # makes it a string
Now it''s bound to a string.
I take it, the parameters to a function (in the above example "l2") are
bound in the definition, rather than as invoked.
So, if I use "l2" thus:
if (l2): # only then does it make it a boolean?
and if I did,
if (l2 = "hello"): # would it become string?
and what if I never used it in the definition body?
Now you''ve lost me. Probably my problem - serves me right for posting
from the pub.
Elucidate please.
我' '允许真正的Python Zen大师这样做 -
< http://effbot.org/zone/python-objects.htm>。
-
干杯,
Simon B,
si *** @ brunningonline.net ,
http://www.brunningonline .net / simon / blog /
这篇关于Python变量在使用时绑定到类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!