Python论坛  - 讨论区

标题:[python-chinese] python class methods identity?

2007年11月23日 星期五 16:22

Roc Zhou chowroc.z+l在gmail.com
星期五 十一月 23 16:22:33 HKT 2007

ÎÒÖ÷ÒªÊÇÏëÖªµÀÁ½¸ö instance µÄ·½·¨ÊDz»ÊÇ»áÕ¼ÓÃÁ½·ÝÄÚ´æ¡£Èç¹ûÊÇ£¬ÄÇÈç¹ûÎÒ
Ϊһ¸ö class ´´½¨´óÁ¿ instance£¬ÄÇÆñ²»ÊÇÒªÕ¼¾Ý´óÁ¿Äڴ棿

On Nov 23, 2007 4:10 PM, limodou <limodou在gmail.com> wrote:

> On Nov 23, 2007 4:06 PM, Roc Zhou <chowroc.z+l在gmail.com> wrote:
> > This is the result comes from the Linux.
> >
> > And the result from Windows is:
> >
> > >>> class Test:
> >     var = 1
> >     def func(self): pass
> > >>> x = Test()
> > >>> y = Test()
> > >>> x.var is y.var
> > True
> > >>> x.func is y.func
> > False
> > >>> id(x.var)
> > 11228488
> > >>> id(y.var)
> > 11228488
> > >>> id(x.func)
> > 14430976
> > >>> id(y.func)
> > 14433656
> >
> ¹À¼ÆÖ»ÄÜ¿´Ô´ÂëÈ¥ÁË¡£²»¹ÜÔõôÑù±È½Ï±äÁ¿µÄ½á¹ûÊÇÔ¤Æڵģ¬¶ø±È½ÏÁ½¸ö·½·¨ËƺõÊÇÓô¦²»´ó¡£
>
> --
> I like python!
> UliPad <>: http://code.google.com/p/ulipad/
> meide <>: http://code.google.com/p/meide/
> My Blog: http://www.donews.net/limodou
>



-- 
------------------------------------------------------------------------
My Projects:
http://sourceforge.net/projects/crablfs
http://www.yourlfs.org/
http://www.yourlfs.org/#ru_data_man
http://www.yourlfs.org/tree.html
http://www.yourlfs.org/sysadm_zh_CN.html
My Blog:
http://chowroc.blogspot.com/
Looking for a space and platform to exert my originalities (for my
projects)...
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20071123/f00a29dc/attachment.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年11月23日 星期五 16:27

@@ askfor在gmail.com
星期五 十一月 23 16:27:50 HKT 2007

ºÃÏñÊǵġ£¡£PythonûÓÐstatic functionһ˵

On 11/23/07, Roc Zhou <chowroc.z+l在gmail.com> wrote:
>
> ÎÒÖ÷ÒªÊÇÏëÖªµÀÁ½¸ö instance µÄ·½·¨ÊDz»ÊÇ»áÕ¼ÓÃÁ½·ÝÄÚ´æ¡£Èç¹ûÊÇ£¬ÄÇÈç¹ûÎÒ
> Ϊһ¸ö class ´´½¨´óÁ¿ instance£¬ÄÇÆñ²»ÊÇÒªÕ¼¾Ý´óÁ¿Äڴ棿
>
> On Nov 23, 2007 4:10 PM, limodou <limodou在gmail.com> wrote:
>
> > On Nov 23, 2007 4:06 PM, Roc Zhou < chowroc.z+l在gmail.com> wrote:
> > > This is the result comes from the Linux.
> > >
> > > And the result from Windows is:
> > >
> > > >>> class Test:
> > >     var = 1
> > >     def func(self): pass
> > > >>> x = Test()
> > > >>> y = Test()
> > > >>> x.var is y.var
> > > True
> > > >>> x.func is y.func
> > > False
> > > >>> id(x.var)
> > > 11228488
> > > >>> id( y.var)
> > > 11228488
> > > >>> id(x.func)
> > > 14430976
> > > >>> id(y.func)
> > > 14433656
> > >
> > ¹À¼ÆÖ»ÄÜ¿´Ô´ÂëÈ¥ÁË¡£²»¹ÜÔõôÑù±È½Ï±äÁ¿µÄ½á¹ûÊÇÔ¤Æڵģ¬¶ø±È½ÏÁ½¸ö·½·¨ËƺõÊÇÓô¦²»´ó¡£
> >
> > --
> > I like python!
> > UliPad <>: http://code.google.com/p/ulipad/
> > meide <>: http://code.google.com/p/meide/
> > My Blog: http://www.donews.net/limodou
> >
>
>
>
> --
> ------------------------------------------------------------------------
> My Projects:
> http://sourceforge.net/projects/crablfs
> http://www.yourlfs.org/
> http://www.yourlfs.org/#ru_data_man
> http://www.yourlfs.org/tree.html
> http://www.yourlfs.org/sysadm_zh_CN.html
> My Blog:
> http://chowroc.blogspot.com/
> Looking for a space and platform to exert my originalities (for my
> projects)...
>
> _______________________________________________
> 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/20071123/e879ec09/attachment.htm 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年11月23日 星期五 16:38

lubiao lubiao.py在gmail.com
星期五 十一月 23 16:38:04 HKT 2007

On Nov 23, 2007 4:22 PM, Roc Zhou <chowroc.z+l at gmail.com> wrote:
> 我主要是想知道两个 instance 的方法是不是会占用两份内存。如果是,那如果我
> 为一个 class 创建大量 instance,那岂不是要占据大量内存?
>
Python 2.5.1 (r251:54863, May 18 2007, 16:56:43)
[GCC 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> class A:
...   def f():
...     pass
...
>>> a = A()
>>> b = A()
>>> dir(a.f)
['__call__', '__class__', '__cmp__', '__delattr__', '__doc__',
'__get__', '__getattribute__', '__hash__', '__init__', '__new__',
'__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__',
'im_class', 'im_func', 'im_self']
>>> id(a.f.im_func)
2146512820
>>> id(b.f.im_func)
2146512820
>>> id(b.f.im_class)
2146527772
>>> id(a.f.im_class)
2146527772
>>> id(a.f.im_self)
2146570860
>>> id(b.f.im_self)
2146570764
>>> id(b)
2146570764
>>> id(a)
2146570860

两个 f 的 im_self  不一样, 其实 im_self 就是 拥有它们的对象

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年11月23日 星期五 16:56

Roc Zhou chowroc.z+l在gmail.com
星期五 十一月 23 16:56:38 HKT 2007

Ã÷°×ÁË¡£

лл '')

On Nov 23, 2007 4:38 PM, lubiao <lubiao.py在gmail.com> wrote:

> On Nov 23, 2007 4:22 PM, Roc Zhou <chowroc.z+l在gmail.com> wrote:
> > ÎÒÖ÷ÒªÊÇÏëÖªµÀÁ½¸ö instance µÄ·½·¨ÊDz»ÊÇ»áÕ¼ÓÃÁ½·ÝÄÚ´æ¡£Èç¹ûÊÇ£¬ÄÇÈç¹ûÎÒ
> > Ϊһ¸ö class ´´½¨´óÁ¿ instance£¬ÄÇÆñ²»ÊÇÒªÕ¼¾Ý´óÁ¿Äڴ棿
> >
> Python 2.5.1 (r251:54863, May 18 2007, 16:56:43)
> [GCC 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)] on cygwin
> Type "help", "copyright", "credits" or "license" for more information.
> >>> class A:
> ...   def f():
> ...     pass
> ...
> >>> a = A()
> >>> b = A()
> >>> dir(a.f)
> ['__call__', '__class__', '__cmp__', '__delattr__', '__doc__',
> '__get__', '__getattribute__', '__hash__', '__init__', '__new__',
> '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__',
> 'im_class', 'im_func', 'im_self']
> >>> id(a.f.im_func)
> 2146512820
> >>> id(b.f.im_func)
> 2146512820
> >>> id(b.f.im_class)
> 2146527772
> >>> id(a.f.im_class)
> 2146527772
> >>> id(a.f.im_self)
> 2146570860
> >>> id(b.f.im_self)
> 2146570764
> >>> id(b)
> 2146570764
> >>> id(a)
> 2146570860
>
> Á½¸ö f µÄ im_self  ²»Ò»Ñù£¬ Æäʵ im_self ¾ÍÊÇ ÓµÓÐËüÃǵĶÔÏó
> _______________________________________________
> 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
>



-- 
------------------------------------------------------------------------
My Projects:
http://sourceforge.net/projects/crablfs
http://www.yourlfs.org/
http://www.yourlfs.org/#ru_data_man
http://www.yourlfs.org/tree.html
http://www.yourlfs.org/sysadm_zh_CN.html
My Blog:
http://chowroc.blogspot.com/
Looking for a space and platform to exert my originalities (for my
projects)...
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20071123/be8da155/attachment.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号