Python论坛  - 讨论区

标题:[python-chinese] 请问 定义了__get__, __set__, __delete__ 这些方法,类的实例如何直接调用这些方法呢?

2007年09月20日 星期四 23:41

萧连衣 xiaolianyi在gmail.com
星期四 九月 20 23:41:28 HKT 2007

hi,´ó¼ÒºÃ:)

¶¨ÒåÁË__get__, __set__, __delete__ ÕâЩ·½·¨,ÀàµÄʵÀýÈçºÎÖ±½Óµ÷ÓÃÕâЩ·½·¨ÄØ?
¶ø²»ÊÇʹÓÃÀàÀ´µ÷ÓÃ(±ÈÈç: Person.__get__(....) or Person.__set__(....))
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070920/a1ced1c6/attachment.html 

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

2007年09月21日 星期五 00:40

Samuel Chi princeofdatamining在gmail.com
星期五 九月 21 00:40:54 HKT 2007

һʱÏë²»ÆðÀ´__get__ºÍ__set__ÓÃÀ´¸ÉʲôÁË,
Ö»¼ÇµÃ__getitem__/__setitem__ÓÃÓÚË÷Òý,
__getattr__/__setattr__ÓÃÓÚ×Ö¶Î

__del__µÄÓ÷¨:
del listdemo[index]»òÕßdel dictdemo[key]»òÕßdel instance.fieldname

ÔÚ07-9-20£¬ÏôÁ¬Ò <xiaolianyi在gmail.com> дµÀ£º
>
> hi,´ó¼ÒºÃ:)
>
> ¶¨ÒåÁË__get__, __set__, __delete__ ÕâЩ·½·¨,ÀàµÄʵÀýÈçºÎÖ±½Óµ÷ÓÃÕâЩ·½·¨ÄØ?
> ¶ø²»ÊÇʹÓÃÀàÀ´µ÷ÓÃ(±ÈÈç: Person.__get__(....) or Person.__set__(....))
>
> _______________________________________________
> 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/20070921/91ee9b80/attachment.html 

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

2007年09月21日 星期五 13:04

萧连衣 xiaolianyi在gmail.com
星期五 九月 21 13:04:04 HKT 2007

ллÐÖµÜ,²»¹ý__delete__ºÍ__del__ÊÇpython¶¨ÒåµÄ²»Í¬·½·¨.
ÎÒÏëÖªµÀ__get__,__delete__ ËûÃǵÄÓ÷¨
*.__delete__µÄÎĵµ¼û:http://docs.python.org/ref/descriptors.html

ÔÚdjangoµÄcontrib\auth\middleware.pyÓж¨ÒåÕâ¸ö·½·¨.

class LazyUser(object):
    def __get__(self, request, obj_type=None):
        if not hasattr(request, '_cached_user'):
            from django.contrib.auth import get_user
            request._cached_user = get_user(request)
        return request._cached_user

class AuthenticationMiddleware(object):
    def process_request(self, request):
        assert hasattr(request, 'session'), "The Django authentication
middleware requires session middleware to be installed. Edit your
MIDDLEWARE_CLASSES setting to insert '
django.contrib.sessions.middleware.SessionMiddleware'."
        request.__class__.user = LazyUser()
        return None

request.userÓкܶàÓÐÓõķ½·¨:Èç,request.user.authenticate(), is_authenticated()

LazyUser() LazyUserʵÀý»¯Ê±ÊÇÔõôµÃµ½Ò»¸öUser,ËûµÄ»º´æÊÇÔõôʵÏÖµÄÄØ?

ÔÚ07-9-21£¬Samuel Chi <princeofdatamining在gmail.com> дµÀ£º
>
> һʱÏë²»ÆðÀ´__get__ºÍ__set__ÓÃÀ´¸ÉʲôÁË,
> Ö»¼ÇµÃ__getitem__/__setitem__ÓÃÓÚË÷Òý,
> __getattr__/__setattr__ÓÃÓÚ×Ö¶Î
>
> __del__µÄÓ÷¨:
> del listdemo[index]»òÕßdel dictdemo[key]»òÕßdel instance.fieldname
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070921/61fb3a22/attachment.html 

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

2007年09月21日 星期五 17:34

leopay leopay在gmail.com
星期五 九月 21 17:34:37 HKT 2007

ÔÚÒ»¸öÒ³ÃæÇëÇóÖеÚÒ»´Îµ÷ÓÃrequest.userÊÇ»áµ÷ÓÃget_user£¬¶øºóÀ´ÔÙµ÷ÓÃrequest.user¾Í»áÖ±½Ó¸ø³öÕâ¸öuserÁË


ÔÚ07-9-21£¬ÏôÁ¬Ò <xiaolianyi在gmail.com> дµÀ£º
>
> ллÐÖµÜ,²»¹ý__delete__ºÍ__del__ÊÇpython¶¨ÒåµÄ²»Í¬·½·¨.
> ÎÒÏëÖªµÀ__get__,__delete__ ËûÃǵÄÓ÷¨
> *.__delete__µÄÎĵµ¼û:http://docs.python.org/ref/descriptors.html
>
> ÔÚdjangoµÄcontrib\auth\middleware.pyÓж¨ÒåÕâ¸ö·½·¨.
>
> class LazyUser(object):
>     def __get__(self, request, obj_type=None):
>         if not hasattr(request, '_cached_user'):
>             from django.contrib.auth import get_user
>             request._cached_user = get_user(request)
>         return request._cached_user
>
> class AuthenticationMiddleware(object):
>     def process_request(self, request):
>         assert hasattr(request, 'session'), "The Django authentication
> middleware requires session middleware to be installed. Edit your
> MIDDLEWARE_CLASSES setting to insert '
> django.contrib.sessions.middleware.SessionMiddleware'."
>         request.__class__.user = LazyUser()
>         return None
>
> request.userÓкܶàÓÐÓõķ½·¨:Èç,request.user.authenticate(), is_authenticated()
>
> LazyUser() LazyUserʵÀý»¯Ê±ÊÇÔõôµÃµ½Ò»¸öUser,ËûµÄ»º´æÊÇÔõôʵÏÖµÄÄØ?
>
> ÔÚ07-9-21£¬Samuel Chi <princeofdatamining在gmail.com > дµÀ£º
> >
> > һʱÏë²»ÆðÀ´__get__ºÍ__set__ÓÃÀ´¸ÉʲôÁË,
> > Ö»¼ÇµÃ__getitem__/__setitem__ÓÃÓÚË÷Òý,
> > __getattr__/__setattr__ÓÃÓÚ×Ö¶Î
> >
> > __del__µÄÓ÷¨:
> > del listdemo[index]»òÕßdel dictdemo[key]»òÕßdel instance.fieldname
>
>
> _______________________________________________
> 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/20070921/cb2bb3a7/attachment.html 

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

2007年09月24日 星期一 20:16

xiao lianyi xiaolianyi在gmail.com
星期一 九月 24 20:16:33 HKT 2007

:)
Àí½âÁË,__get__¶¨ÒåÁËÒÔºó,python»áÈ¥×Ô¶¯µ÷ÓÃ,ºÇºÇ,ºÜˬ.

request.__class__.user = LazyUser()
²»¹ý»¹ÓÐÒ»¸ö²»Àí½âµÄµØ·½,django¸ÉÂðÒªÓÃÀàµÄÊôÐÔÀ´±£´æuserÄØ?Ϊʲô²»ÓÃʵÀýÄØ?


ÔÚ07-9-21£¬leopay <leopay在gmail.com> дµÀ£º
>
> ÔÚÒ»¸öÒ³ÃæÇëÇóÖеÚÒ»´Îµ÷ÓÃrequest.userÊÇ»áµ÷ÓÃget_user£¬¶øºóÀ´ÔÙµ÷ÓÃrequest.user¾Í»áÖ±½Ó¸ø³öÕâ¸öuserÁË
>
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070924/fee70559/attachment.html 

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

2007年09月24日 星期一 23:28

beck917 beck917在gmail.com
星期一 九月 24 23:28:47 HKT 2007

²¹Ï°Ï³£Ê¶.mark

ÔÚ07-9-24£¬xiao lianyi <xiaolianyi在gmail.com> дµÀ£º
>
> :)
> Àí½âÁË,__get__¶¨ÒåÁËÒÔºó,python»áÈ¥×Ô¶¯µ÷ÓÃ,ºÇºÇ,ºÜˬ.
>
> request.__class__.user = LazyUser()
> ²»¹ý»¹ÓÐÒ»¸ö²»Àí½âµÄµØ·½,django¸ÉÂðÒªÓÃÀàµÄÊôÐÔÀ´±£´æuserÄØ?Ϊʲô²»ÓÃʵÀýÄØ?
>
>
> ÔÚ07-9-21£¬leopay <leopay在gmail.com> дµÀ£º
> >
> > ÔÚÒ»¸öÒ³ÃæÇëÇóÖеÚÒ»´Îµ÷ÓÃrequest.userÊÇ»áµ÷ÓÃget_user£¬¶øºóÀ´ÔÙµ÷ÓÃrequest.user¾Í»áÖ±½Ó¸ø³öÕâ¸öuserÁË
> >
>
> _______________________________________________
> 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/20070924/f509c0dc/attachment.html 

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

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

    你的回复:

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

    Zeuux © 2025

    京ICP备05028076号