2007年04月11日 星期三 09:52
ÕýÎÄ£º
ÎÒÔÚa.py¶¨ÒåÁËÒ»¸öÈ«¾Ö±äÁ¿gUser,gUserÔÚaÖб»¸³Öµ¡£
ÔÚb.pyÖУº
from a import gUser
´Ëʱ£¬gUserÓÐʱµÈÓÚÔÚaÖи³µÄÖµ£¬ÓÐʱΪ¿Õ¡£
²ÉÓÃÈ«¾ÖÀàҲʧ°ÜÁË£º
class User(object):
UserName = ''
def getUser(self):
return UserName
def setUser(self, name):
UserName = name
ÎÒµÄÄ¿µÄÊÇÔÚ¶à¸öpyÖй²ÏíÒ»¸öÈ«¾Ö±äÁ¿¡£
ÎÒÒÑ×ÐϸÔĶÁǰ¼¸Ìì¹ØÓÚ´ËÎÊÌâµÄÌÖÂÛ£¬²¢Î´½â¾öÎÊÌâ¡£
лл¡£
mvfirst
2007-04-11
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070411/deb205eb/attachment.html
2007年04月11日 星期三 09:57
On 4/11/07, mvfirst <mvfirst在126.com> wrote: > > > > 正文: > 我在a.py定义了一个全局变量gUser,gUser在a中被赋值。 > 在b.py中: > from a import gUser > 此时,gUser有时等于在a中赋的值,有时为空。 > > 采用全局类也失败了: > class User(object): > UserName = '' > def getUser(self): > return UserName > def setUser(self, name): > UserName = name > > 我的目的是在多个py中共享一个全局变量。 > 我已仔细阅读前几天关于此问题的讨论,并未解决问题。 > > 谢谢。 > 怎么叫有时行,有时不行,有例子吗?另外建议不要使用from a import gUser,而是使用 import a 使用 a.gUser为好。 -- I like python! UliPad <>: http://wiki.woodpecker.org.cn/moin/UliPad My Blog: http://www.donews.net/limodou
2007年04月11日 星期三 10:03
#a.py
user = 'some user'
def test():
print user
import b
b.change()
print user
# end a.py
#b.py
import a
def change():
a.user = 'another user'
if __name__ == '__main__':
a.test()
# end b.py
run b.py :
some user
another user
On 4/11/07, mvfirst <mvfirst at 126.com> wrote:
>
> 正文:
> 我在a.py定义了一个全局变量gUser,gUser在a中被赋值。
> 在b.py中:
> from a import gUser
> 此时,gUser有时等于在a中赋的值,有时为空。
>
> 采用全局类也失败了:
> class User(object):
> UserName = ''
> def getUser(self):
> return UserName
> def setUser(self, name):
> UserName = name
>
> 我的目的是在多个py中共享一个全局变量。
> 我已仔细阅读前几天关于此问题的讨论,并未解决问题。
>
> 谢谢。
>
>
> ------------------------------
> mvfirst
> 2007-04-11
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
--
http://codeplayer.blogspot.com/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://python.cn/pipermail/python-chinese/attachments/20070411/4632193f/attachment.htm
2007年04月11日 星期三 10:06
ÓÐʱȫ¾Ö±äÁ¿ÊÇÓÐÊýÖµµÄ£¬ÓÐʱÊǿյ쬾ßÌ庬Òå½âÊÍÈçÏ£º
login.py£º
gUser=""
def index(request):
global gUser
g_User="aaaaaaaaaaaaa"
return HttpResponseRedirect('/homepage/')
homepage.py£º
from django.http import HttpResponse
import login £¨ËµÃ÷£º from login import gUser Ò²ÊÔÑéÁË£¬Ò²²»ÐУ©
def index(request):
return HttpResponse(login.gUser)
´Ëʱ£¬ÓÐʱ´°¿Ú¿ÉÒÔÏÔʾ³ö ¡°aaaaaaa.."£¬ÓÐʱΪ¿Õ°×¡£ËùÒÔ£¬gUserÓÐʱÓÐÊýÖµ£¬ÓÐʱûÓУ¬¹À¼Æ±»ÊÍ·ÅÁË£¿
limodou˵£º
Ôõô½ÐÓÐʱÐУ¬ÓÐʱ²»ÐУ¬ÓÐÀý×ÓÂð£¿ÁíÍ⽨Ò鲻ҪʹÓÃfrom a import gUser£¬¶øÊÇʹÓÃ
import a
ʹÓÃ
a.gUserΪºÃ¡£
mvfirst
2007-04-11
·¢¼þÈË£º limodou
·¢ËÍʱ¼ä£º 2007-04-11 09:58:23
ÊÕ¼þÈË£º python-chinese在lists.python.cn
³ËÍ£º
Ö÷Ì⣺ Re: [python-chinese]ÀûÓÃÈ«¾Ö±äÁ¿ºÍÈ«¾ÖÀàʵÏֵĶàpy¹²Ïí±äÁ¿Ê§°Ü£¨Ç°¸öÓʼþÂÒÂë£¬ÖØ·¢£©
On 4/11/07, mvfirst <mvfirst在126.com > wrote:
>
>
>
> ÕýÎÄ£º
> ÎÒÔÚa.py¶¨ÒåÁËÒ»¸öÈ«¾Ö±äÁ¿gUser,gUserÔÚaÖб»¸³Öµ¡£
> ÔÚb.pyÖУº
> from a import gUser
> ´Ëʱ£¬gUserÓÐʱµÈÓÚÔÚaÖи³µÄÖµ£¬ÓÐʱΪ¿Õ¡£
>
> ²ÉÓÃÈ«¾ÖÀàҲʧ°ÜÁË£º
> class User(object):
> UserName = ''
> def getUser(self):
> return UserName
> def setUser(self, name):
> UserName = name
>
> ÎÒµÄÄ¿µÄÊÇÔÚ¶à¸öpyÖй²ÏíÒ»¸öÈ«¾Ö±äÁ¿¡£
> ÎÒÒÑ×ÐϸÔĶÁǰ¼¸Ìì¹ØÓÚ´ËÎÊÌâµÄÌÖÂÛ£¬²¢Î´½â¾öÎÊÌâ¡£
>
> лл¡£
>
--
I like python!
UliPad < >: http://wiki.woodpecker.org.cn/moin/UliPad
My Blog: http://www.donews.net/limodou
_______________________________________________
python-chinese
Post: send python-chinese在lists.python.cn
Subscribe: send subscribe to python-chinese-request在lists.python.cn
Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn
Detail Info: http://python.cn/mailman/listinfo/python-chinese
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070411/88a73c44/attachment.htm
2007年04月11日 星期三 10:10
On 4/11/07, mvfirst <mvfirst在126.com> wrote: > > > 有时全局变量是有数值的,有时是空的,具体含义解释如下: > > login.py: > gUser="" > def index(request): > global gUser > g_User="aaaaaaaaaaaaa" 一会是gUser,一会是g_User,到底是哪个? > return HttpResponseRedirect('/homepage/') > > homepage.py: > from django.http import HttpResponse > import login (说明: from login import gUser 也试验了,也不行) > def index(request): > return HttpResponse(login.gUser) > > 此时,有时窗口可以显示出 "aaaaaaa..",有时为空白。所以,gUser有时有数值,有时没有,估计被释放了? > -- Best Regards, Leo Jay
2007年04月11日 星期三 10:21
>
> 有时全局变量是有数值的,有时是空的,具体含义解释如下:
>
> login.py:
> gUser=""
> def index(request):
> global gUser
> g_User="aaaaaaaaaaaaa"
> return HttpResponseRedirect('/homepage/')
>
> homepage.py:
> from django.http import HttpResponse
> import login (说明: from login import gUser 也试验了,也不行)
> def index(request):
> return HttpResponse(login.gUser)
>
> 此时,有时窗口可以显示出 "aaaaaaa..",有时为空白。所以,gUser有时有数值,有时没有,估计被释放了?
>
= ="""
具体发生了什么我也说不清,这取决与 django 的实现甚至部署的环境。
总之 web 程序里面别这么做,这是个多线程甚至多进程的环境,如果多次访问是处于同一个虚拟机的话,全局变量可能还有效,就算这样对全局变量的写也得加锁。
想要在不同 http 请求之间共享数据,请使用 session !
你想做用户验证,django 也有现成的东西,用户注册都有现成的东西。
--
http://codeplayer.blogspot.com/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://python.cn/pipermail/python-chinese/attachments/20070411/e197f171/attachment.htm
2007年04月11日 星期三 10:22
¶¼ÊÇgUser,Óʼþд´íÁË£¬µ«.pyÖÐûд´í£¬ÒòΪÎÒ¾«¼òÁË´úÂë·Åµ½ÓʼþÖУ¬ËùÒÔ±ÊÎóÁË¡£ mvfirst 2007-04-11 ·¢¼þÈË£º Leo Jay ·¢ËÍʱ¼ä£º 2007-04-11 10:11:23 ÊÕ¼þÈË£º python-chinese在lists.python.cn ³ËÍ£º Ö÷Ì⣺ Re: [python-chinese]ÀûÓÃÈ«¾Ö±äÁ¿ºÍÈ«¾ÖÀàʵÏֵĶàpy¹²Ïí±äÁ¿Ê§°Ü£¨Ç°¸öÓʼþÂÒÂë£¬ÖØ·¢£© On 4/11/07, mvfirst <mvfirst在126.com > wrote: > > > ÓÐʱȫ¾Ö±äÁ¿ÊÇÓÐÊýÖµµÄ£¬ÓÐʱÊǿյ쬾ßÌ庬Òå½âÊÍÈçÏ£º > > login.py£º > gUser="" > def index(request): > global gUser > g_User="aaaaaaaaaaaaa" Ò»»áÊÇgUser£¬Ò»»áÊÇg_User£¬µ½µ×ÊÇÄĸö£¿ > return HttpResponseRedirect('/homepage/') > > homepage.py£º > from django.http import HttpResponse > import login £¨ËµÃ÷£º from login import gUser Ò²ÊÔÑéÁË£¬Ò²²»ÐУ© > def index(request): > return HttpResponse(login.gUser) > > ´Ëʱ£¬ÓÐʱ´°¿Ú¿ÉÒÔÏÔʾ³ö "aaaaaaa.."£¬ÓÐʱΪ¿Õ°×¡£ËùÒÔ£¬gUserÓÐʱÓÐÊýÖµ£¬ÓÐʱûÓУ¬¹À¼Æ±»ÊÍ·ÅÁË£¿ > -- Best Regards, Leo Jay _______________________________________________ python-chinese Post: send python-chinese在lists.python.cn Subscribe: send subscribe to python-chinese-request在lists.python.cn Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn Detail Info: http://python.cn/mailman/listinfo/python-chinese -------------- 下一部分 -------------- Ò»¸öHTML¸½¼þ±»ÒƳý... URL: http://python.cn/pipermail/python-chinese/attachments/20070411/d16a94ba/attachment.html
2007年04月11日 星期三 11:01
djangoµÄÈÏÖ¤¿âÊǺܺ㬵«ÎÒ»¹ÓбðµÄÐèÇó£¬ËùÒÔÔݲ»²ÉÓᣠ¿ÉÒÔÅ׿ª¾ßÌåÓÃ;£¬Ö»ÊÇÏë½â¾öÒ»¸ö¶àpy¹²Ïí±äÁ¿µÄÎÊÌâ¡£ mvfirst 2007-04-11 ·¢¼þÈË£º »ÆÒã ·¢ËÍʱ¼ä£º 2007-04-11 10:21:51 ÊÕ¼þÈË£º python-chinese在lists.python.cn ³ËÍ£º Ö÷Ì⣺ Re: [python-chinese]ÀûÓÃÈ«¾Ö±äÁ¿ºÍÈ«¾ÖÀàʵÏֵĶàpy¹²Ïí±äÁ¿Ê§°Ü£¨Ç°¸öÓʼþÂÒÂë£¬ÖØ·¢£© ÓÐʱȫ¾Ö±äÁ¿ÊÇÓÐÊýÖµµÄ£¬ÓÐʱÊǿյ쬾ßÌ庬Òå½âÊÍÈçÏ£º login.py£º gUser="" def index(request): global gUser g_User="aaaaaaaaaaaaa" return HttpResponseRedirect('/homepage/') homepage.py£º from django.http import HttpResponse import login £¨ËµÃ÷£º from login import gUser Ò²ÊÔÑéÁË£¬Ò²²»ÐУ© def index(request): return HttpResponse(login.gUser) ´Ëʱ£¬ÓÐʱ´°¿Ú¿ÉÒÔÏÔʾ³ö "aaaaaaa.."£¬ÓÐʱΪ¿Õ°×¡£ËùÒÔ£¬gUserÓÐʱÓÐÊýÖµ£¬ÓÐʱûÓУ¬¹À¼Æ±»ÊÍ·ÅÁË£¿ = =""" ¾ßÌå·¢ÉúÁËʲôÎÒҲ˵²»Ç壬ÕâÈ¡¾öÓë django µÄʵÏÖÉõÖÁ²¿ÊðµÄ»·¾³¡£ ×ÜÖ® web ³ÌÐòÀïÃæ±ðÕâô×ö£¬ÕâÊǸö¶àÏß³ÌÉõÖÁ¶à½ø³ÌµÄ»·¾³£¬Èç¹û¶à´Î·ÃÎÊÊÇ´¦ÓÚͬһ¸öÐéÄâ»úµÄ»°£¬È«¾Ö±äÁ¿¿ÉÄÜ»¹ÓÐЧ£¬¾ÍËãÕâÑù¶ÔÈ«¾Ö±äÁ¿µÄдҲµÃ¼ÓËø¡£ ÏëÒªÔÚ²»Í¬ http ÇëÇóÖ®¼ä¹²ÏíÊý¾Ý£¬ÇëʹÓà session £¡ ÄãÏë×öÓû§ÑéÖ¤£¬django Ò²ÓÐÏֳɵĶ«Î÷£¬Óû§×¢²á¶¼ÓÐÏֳɵĶ«Î÷¡£ -- http://codeplayer.blogspot.com/ -------------- 下一部分 -------------- Ò»¸öHTML¸½¼þ±»ÒƳý... URL: http://python.cn/pipermail/python-chinese/attachments/20070411/5ad71a71/attachment.html
2007年04月11日 星期三 11:30
你的意思是在django里面多py共享变量吧? On 4/11/07, mvfirst <mvfirst在126.com> wrote: > > django的认证库是很好,但我还有别的需求,所以暂不采用。 > 可以抛开具体用途,只是想解决一个多py共享变量的问题。 > > ------------------------------ > mvfirst > 2007-04-11 > ------------------------------ > *发件人:* 黄毅 > *发送时间:* 2007-04-11 10:21:51 > *收件人:* python-chinese在lists.python.cn > *抄送:* > *主题:* Re: [python-chinese]利用全局变量和全局类实现的多py共享变量失败(前个邮件乱码,重发) > > > > 有时全局变量是有数值的,有时是空的,具体含义解释如下: > > > > login.py: > > gUser="" > > def index(request): > > global gUser > > g_User="aaaaaaaaaaaaa" > > return HttpResponseRedirect('/homepage/') > > > > homepage.py: > > from django.http import HttpResponse > > import login (说明: from login import gUser 也试验了,也不行) > > def index(request): > > return HttpResponse(login.gUser) > > > > 此时,有时窗口可以显示出 "aaaaaaa..",有时为空白。所以,gUser有时有数值,有时没有,估计被释放了? > > > > = =""" > > 具体发生了什么我也说不清,这取决与 django 的实现甚至部署的环境。 > > 总之 web > 程序里面别这么做,这是个多线程甚至多进程的环境,如果多次访问是处于同一个虚拟机的话,全局变量可能还有效,就算这样对全局变量的写也得加锁。 > > 想要在不同 http 请求之间共享数据,请使用 session ! > > 你想做用户验证,django 也有现成的东西,用户注册都有现成的东西。 > > -- > http://codeplayer.blogspot.com/ > > _______________________________________________ > python-chinese > Post: send python-chinese在lists.python.cn > Subscribe: send subscribe to python-chinese-request在lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese > -- Best Regards, Archer Ming Zhe Huang -------------- 下一部分 -------------- 一个HTML附件被移除... URL: http://python.cn/pipermail/python-chinese/attachments/20070411/f1943433/attachment.htm
Zeuux © 2025
京ICP备05028076号