又困惑了:非常新手的问题 [英] Confused yet again: Very Newbie Question

查看:64
本文介绍了又困惑了:非常新手的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我不能在另一个类中更改变量的值,

当我通过参数列表传递它时。


我是确定我是傻瓜,但我认为传递的物品是读/

写的


例如

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

#!/ usr / bin / python

class one():#my Global Vars

fred =''fred''


三级():

def createJoe(self,myName):

c1 = 1 ()

myName =''Joe''#*********************问为什么这个

不改变''一级'中的变量fred

print''三(本地):''+ myName +''三(全球):''+

c1.fred


def main():

c1 =一()

c3 =三()

c3.createJoe(c1.fred)

if __name__ ==''__ main__'':main()


结果:

三(本地):乔三(全球):fred

$'$ b''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' >
''crecreJoe'',即使我已经通过''一级''''''''''''''''''''''''''''''$ $ $ $ $'$' b $ b我希望这是有道理的。


我认为你不必区分''byvar''和

''byref' '就像在Basic。


谢谢


Richard

解决方案

< blockquote> mcl写道:


为什么我不能在另一个类中更改变量的值,

当我通过它时一个参数列表。


我确定我是傻瓜,但我认为传递的对象是读/




在Python中,有一些绑定到对象的名称。做foo = bar

然后foo = spam (重新)绑定名称foo与

垃圾邮件相同的对象一定会。这不会对

与bar绑定到同一对象的任何其他名称产生任何影响。


eg

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

#!/ usr / bin / python


class one() :#my Global Vars

fred =''fred''


class three():

def createJoe(self, myName):



这里,本地名称myName绑定到相同的字符串对象

one.fred。


c1 = one()

myName =''乔''#*********************问为什么这个

不会改变''一级'变量fred '



在这里,你重新绑定本地名称myName字符串对象''Joe''。

这并没有改变one.fred的约束。


print' '三(本地):''+ myName +''三(全球):''+

c1.fred


def main():

c1 =一()

c3 =三()

c3.createJoe(c1.fred)


if __name__ ==''__ main__'':main()


结果:

三(本地):Joe Three(全球):fred

$'$''''''''''''''''''''''''''''''''''''''''''' 'createJoe'',即使我已经通过了''一级'''''''''''''''''''$ $ $ $'$'$ b $'''''''''是有道理的。


我认为你不必像'基本'那样区分''byvar''和

''byref''。 br />

谢谢


Richa rd



在线有几篇关于此的好文章。下面的那个是长期的b $ b,有ASCII艺术,但我不记得我是否喜欢它。


< http:// starship。 python.net/crew/mwh/hacks/objectthink.html>


BTW,在这个例子中,似乎没有必要让你成为

使用类。此外,你应该总是使用新式的类,除非你有一个特定的理由没有


>>类MyClass(对象):



-


当你调用c3.createJoe(c1.fred)时,你将把存储在c1.fred中的

值的副本传递给你的函数。 Python按值传递函数

参数。该函数不会破坏性地修改其

参数;你必须明确表示你打算修改

对象:


class one():

fred =''fred' '


三级():

def createJoe(self,myName):

return" Joe"


def main():

c1 = 1()

c3 = 3()

c1。 fred = c3.createJoe()#修改c1'的属性fred,使用来自c3.createJoe的

返回值


On 7 Jul,13:09,Jeff< jeffo ... @ gmail.comwrote:


当你调用c3.createJoe(c1.fred)时,你正在传递一个存储在c1.fred中的

值的副本到您的函数。 * Python按值传递函数

参数。 *该函数不会破坏性地修改其

参数;你必须明确表示你打算修改

对象:


class one():

* * fred ='' fred''


三级():

* * def createJoe(self,myName):

* * * *返回乔


def main():

* * c1 = 1()

* * c3 = 3 ()

* * c1.fred = c3.createJoe()#修改c1'的属性fred,使用来自c3.createJoe $ b $的

返回值b



非常感谢您的回复。


两个问题:

一:
我对类的使用是因为我想要两个类一个用于全局

变量,一个用于全局函数。

一个函数可以设置多个全局变量值,那么

最好的方法是什么,因为''return''似乎只能设置一个

值。

两个:

我很抱歉,但我不明白定义一个班级的重要性

as:


>> class MyClass(object ):



什么是对象?


我在Google App Engine中使用python ,我只有Alex

Martelli对Python 2.2的预订,如果它们有任何相关性吗?


再次感谢您的帮助。我现在明白我的错误是什么,并且为什么我没有得到我预期的结果。


Richard


Why can I not the change the value of a variable in another class,
when I have passed it via a parameter list.

I am sure I am being stupid, but I thought passed objects were Read/
Write

eg
------------------------------------------------------------
#!/usr/bin/python

class one(): #my Global Vars
fred = ''fred''

class three():
def createJoe(self, myName):
c1 = one()
myName = ''Joe'' #********************* Question why does this
not change variable fred in ''class one''
print ''Three(Local): '' + myName + '' Three(Global): '' +
c1.fred

def main():
c1 = one()
c3 =three()
c3.createJoe(c1.fred)
if __name__ == ''__main__'' : main()

Results:
Three(Local): Joe Three(Global): fred

''fred'' in ''class one'' does not get changed to ''joe'' in ''class three''
''createJoe'', even though I have passed ''class one'' ''fred'' to
''createJoe''

I hope this makes sense.

I did not think you had to make the distinction between ''byvar'' and
''byref'' as in Basic.

Thanks

Richard

解决方案

mcl wrote:

Why can I not the change the value of a variable in another class,
when I have passed it via a parameter list.

I am sure I am being stupid, but I thought passed objects were Read/
Write

In Python, there are names which are bound to objects. Doing "foo = bar"
and then "foo = spam" (re)binds the name "foo" to the same object as
"spam" is bound to. This doesn''t have any effect on any other names that
were bound to the same object as "bar".

eg
------------------------------------------------------------
#!/usr/bin/python

class one(): #my Global Vars
fred = ''fred''

class three():
def createJoe(self, myName):

Here, the local name "myName" is bound to the same string object as
one.fred.

c1 = one()
myName = ''Joe'' #********************* Question why does this
not change variable fred in ''class one''

Here, you rebind the local name "myName" to the string object ''Joe''.
This doesn''t change what one.fred is bound to.

print ''Three(Local): '' + myName + '' Three(Global): '' +
c1.fred

def main():
c1 = one()
c3 =three()
c3.createJoe(c1.fred)
if __name__ == ''__main__'' : main()

Results:
Three(Local): Joe Three(Global): fred

''fred'' in ''class one'' does not get changed to ''joe'' in ''class three''
''createJoe'', even though I have passed ''class one'' ''fred'' to
''createJoe''

I hope this makes sense.

I did not think you had to make the distinction between ''byvar'' and
''byref'' as in Basic.

Thanks

Richard

There are a couple good articles about this online. The one below is
lengthy and has ASCII art, but I don''t remember if I liked it.

<http://starship.python.net/crew/mwh/hacks/objectthink.html>

BTW, in this example, there doesn''t seem to be any need for you to be
using classes. Also, you should always use new-style classes unless you
have a specific reason not to

>>class MyClass(object):

--


When you call c3.createJoe(c1.fred), you are passing a copy of the
value stored in c1.fred to your function. Python passes function
parameters by value. The function will not destructively modify its
arguments; you must expliticly state your intention to modify an
object:

class one():
fred = ''fred''

class three():
def createJoe(self, myName):
return "Joe"

def main():
c1 = one()
c3 = three()
c1.fred = c3.createJoe() # Modify c1''s attribute, fred, with the
return value from c3.createJoe


On 7 Jul, 13:09, Jeff <jeffo...@gmail.comwrote:

When you call c3.createJoe(c1.fred), you are passing a copy of the
value stored in c1.fred to your function. *Python passes function
parameters by value. *The function will not destructively modify its
arguments; you must expliticly state your intention to modify an
object:

class one():
* * fred = ''fred''

class three():
* * def createJoe(self, myName):
* * * * return "Joe"

def main():
* * c1 = one()
* * c3 = three()
* * c1.fred = c3.createJoe() # Modify c1''s attribute, fred, with the
return value from c3.createJoe

Thank you very much for your helpful replies.

Two questions:
One:
My use of classes is because I want two classes one for global
variables and one for global functions.
A function may set multiple global variable values, so what is the
best way to do this as ''return'' only appears to be able to set one
value.
Two:
I am sorry but I do not understand the significance defining a Class
as:

>>class MyClass(object):

what is object ?

I am using python with Google App Engine, and I only have Alex
Martelli''s book up to Python 2.2, if they has any relevance ?

Thanks again for your help. I now understand what my mistakes were and
why I was not getting the results I had expected.

Richard


这篇关于又困惑了:非常新手的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆