Python论坛  - 讨论区

标题:[python-chinese] Just a test(please ignore it)

2005年06月06日 星期一 15:39

Jacob Fan jacob at exoweb.net
Mon Jun 6 15:39:34 HKT 2005

to see if the list works now.


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

2005年06月06日 星期一 15:57

CaiJianFeng caijf at ec.sh.necsl.com.cn
Mon Jun 6 15:57:04 HKT 2005

上次面试的时候一个老外问的,我说在getStaffList()中对于staffList做个
protected-copy,他没反应。
大家帮着想一想!谢谢啊!

pilbic class Company {
    private List staffList;

    public List void getStaffList() {
        return staffList;
    }

.....
}

public class EvilAgency() {

    public void buildContact() {

        List stafflist = myCompany.getStaffList();
        .....
        stafflist.clear(); //how to prevent this occuring?
    .....
    }
......
}


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

2005年06月06日 星期一 16:07

Zoom Quiet zoom.quiet at gmail.com
Mon Jun 6 16:07:57 HKT 2005

"""
Not Found
The requested URL /pipermail/python-chinese/ was not found on this server.
"""
呜乎哀哉!还有首页的快速退订入口怎么也没有了??

2005/6/6, Jacob Fan <jacob at exoweb.net>:
> to see if the list works now.
> 
> _______________________________________________
> python-chinese list
> python-chinese at lists.python.cn
> http://python.cn/mailman/listinfo/python-chinese
> 


-- 
[Time is unimportant, only life important!]

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

2005年06月06日 星期一 16:54

Neo Chan (netkiller) neo.chen at achievo.com
Mon Jun 6 16:54:34 HKT 2005

List stafflist = myCompany.getStaffList();
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
应该是下面这样吧???
List stafflist = Company.getStaffList();

-----Original Message-----
From: python-chinese-bounces at lists.python.cn [mailto:python-chinese-bounces at lists.python.cn] On Behalf Of CaiJianFeng
Sent: Monday, June 06, 2005 3:57 PM
To: python-chinese at lists.python.cn
Subject: [python-chinese] How to prevent calling the clear method of thelist?

上次面试的时候一个老外问的,我说在getStaffList()中对于staffList做个
protected-copy,他没反应。
大家帮着想一想!谢谢啊!

pilbic class Company {
    private List staffList;

    public List void getStaffList() {
        return staffList;
    }

.....
}

public class EvilAgency() {

    public void buildContact() {

        List stafflist = myCompany.getStaffList();
        .....
        stafflist.clear(); //how to prevent this occuring?
    .....
    }
......
}

_______________________________________________
python-chinese list
python-chinese at lists.python.cn
http://python.cn/mailman/listinfo/python-chinese


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

2005年06月06日 星期一 17:04

CaiJianFeng caijf at ec.sh.necsl.com.cn
Mon Jun 6 17:04:28 HKT 2005

不好意思,说的不够清楚,myCompany是Company的一个实例,就代表某个公司吧。

EvilAgency在这里就是个保险机构,比如某个公司要为所有的员工上意外保险,就要和该公司每
个员工签署一份合同。

----- Original Message ----- 
From: "Neo Chan (netkiller)" <neo.chen at achievo.com>
To: <python-chinese at lists.python.cn>
Sent: Monday, June 06, 2005 4:54 PM
Subject: RE: [python-chinese] How to prevent calling the clear method
ofthelist?


> List stafflist = myCompany.getStaffList();
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> 应该是下面这样吧???
> List stafflist = Company.getStaffList();
>
> -----Original Message-----
> From: python-chinese-bounces at lists.python.cn
[mailto:python-chinese-bounces at lists.python.cn] On Behalf Of CaiJianFeng
> Sent: Monday, June 06, 2005 3:57 PM
> To: python-chinese at lists.python.cn
> Subject: [python-chinese] How to prevent calling the clear method of
thelist?
>
> 上次面试的时候一个老外问的,我说在getStaffList()中对于staffList做个
> protected-copy,他没反应。
> 大家帮着想一想!谢谢啊!
>
> pilbic class Company {
>     private List staffList;
>
>     public List void getStaffList() {
>         return staffList;
>     }
>
> .....
> }
>
> public class EvilAgency() {
>
>     public void buildContact() {
>
>         List stafflist = myCompany.getStaffList();
>         .....
>         stafflist.clear(); //how to prevent this occuring?
>     .....
>     }
> ......
> }
>
> _______________________________________________
> python-chinese list
> python-chinese at lists.python.cn
> http://python.cn/mailman/listinfo/python-chinese
>
> _______________________________________________
> python-chinese list
> python-chinese at lists.python.cn
> http://python.cn/mailman/listinfo/python-chinese


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

2005年06月06日 星期一 23:48

Qiangning Hong hongqn at gmail.com
Mon Jun 6 23:48:02 HKT 2005

CaiJianFeng wrote:
> 上次面试的时候一个老外问的,我说在getStaffList()中对于staffList做个
> protected-copy,他没反应。
> 大家帮着想一想!谢谢啊!
> 
> pilbic class Company {
>     private List staffList;
> 
>     public List void getStaffList() {
>         return staffList;
>     }
> 
> .....
> }
> 
> public class EvilAgency() {
> 
>     public void buildContact() {
> 
>         List stafflist = myCompany.getStaffList();
>         .....
>         stafflist.clear(); //how to prevent this occuring?
>     .....
>     }
> ......
> }
> 

做copy系统开销太大了,可以让getStaffList不直接返回staffList,而是返回一
个不具有clear方法的代理对象,可以考虑使用delegate pattern。

用python描述如下:

class ImmutableList(object):
    def __init__(self, obj):
        self.__obj = obj

    def __getattr__(self, attr):
        return getattr(self.__obj, attr)

    def clear(self):
        raise AttributeError

class Company(object):
    def getStaffList(self):
        return ImmutableList(self.__staffList)


-- 
Qiangning Hong

 ___________________________________________________________
/  No, I do not know what the Schadenfreude is. Please tell \
| me, because I'm dying to know.                            |
|                                                           |
| -- Homer Simpson                                          |
|                                                           |
\ When Flanders Failed                                      /
 -----------------------------------------------------------
    \
     \
      \
  ___       _____     ___
 /   \     /    /|   /   \
|     |   /    / |  |     |
|     |  /____/  |  |     |
|     |  |    |  |  |     |
|     |  | {} | /   |     |
|     |  |____|/    |     |
|     |    |==|     |     |
|      \___________/      |
|                         |
|                         |

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

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

    你的回复:

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

    Zeuux © 2025

    京ICP备05028076号