一道关于 Python 点操作符的笔试题

244 views
Skip to first unread message

kougazhang

unread,
Jan 15, 2019, 12:18:44 AM1/15/19
to python-cn(华蟒用户组,CPyUG 邮件列表)
大家好, 我最近在面试的时候, 遇到了一道题不会做. 求教各位大神.

---- 现在笔试已经结束了, 我就是想学习一下,到底咋做.

题目如下:

请写一个类继承dict类,支持.方式访问
mydict = Mydict()
mydict.a = 1
mydict.b = 2
mydict.c.d = 3
print mydict
print mydict.c
输出:
{'a': 1, 'c': {'d': 3}, 'b': 2}
{'d': 3}

Chunlin Zhang

unread,
Jan 15, 2019, 12:40:09 AM1/15/19
to python-cn`CPyUG`华蟒用户组

--
邮件来自: `CPyUG`华蟒用户组(中文Python技术邮件列表)
规则: http://code.google.com/p/cpyug/wiki/PythonCn
详情: http://code.google.com/p/cpyug/wiki/CpyUg
严正: 理解列表! 智慧提问! http://wiki.woodpecker.org.cn/moin/AskForHelp
---
您收到此邮件是因为您订阅了Google网上论坛上的“python-cn(华蟒用户组,CPyUG 邮件列表)”群组。
要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到python-cn+...@googlegroups.com
要发帖到此群组,请发送电子邮件至pyth...@googlegroups.com
要查看更多选项,请访问https://groups.google.com/d/optout

Shell Xu

unread,
Jan 15, 2019, 12:41:45 AM1/15/19
to CUPG
我建议你远离食死徒。

On Tue, Jan 15, 2019 at 1:18 PM kougazhang <kouga...@gmail.com> wrote:
--
邮件来自: `CPyUG`华蟒用户组(中文Python技术邮件列表)
规则: http://code.google.com/p/cpyug/wiki/PythonCn
详情: http://code.google.com/p/cpyug/wiki/CpyUg
严正: 理解列表! 智慧提问! http://wiki.woodpecker.org.cn/moin/AskForHelp
---
您收到此邮件是因为您订阅了Google网上论坛上的“python-cn(华蟒用户组,CPyUG 邮件列表)”群组。
要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到python-cn+...@googlegroups.com
要发帖到此群组,请发送电子邮件至pyth...@googlegroups.com
要查看更多选项,请访问https://groups.google.com/d/optout


--
彼節者有間,而刀刃者無厚;以無厚入有間,恢恢乎其於游刃必有餘地矣。
blog: http://shell909090.org/

zc grey

unread,
Jan 15, 2019, 12:43:38 AM1/15/19
to pyth...@googlegroups.com
食死徒这个名字很棒耶!
远离+1

vicalloy

unread,
Jan 15, 2019, 12:48:07 AM1/15/19
to pyth...@googlegroups.com
应当是考python的内置方法的使用,类似下面的处理

class MyDict(dict):
    def __getattribute__(self, name):
        try:
            return super(MyDict, self).__getattribute__(name)
        except:
            if name in self.keys():
                return self[name]
        return None

m = MyDict()
m['a'] = 'aa'
print m.a

On Tue, Jan 15, 2019 at 1:18 PM kougazhang <kouga...@gmail.com> wrote:
--
邮件来自: `CPyUG`华蟒用户组(中文Python技术邮件列表)
规则: http://code.google.com/p/cpyug/wiki/PythonCn
详情: http://code.google.com/p/cpyug/wiki/CpyUg
严正: 理解列表! 智慧提问! http://wiki.woodpecker.org.cn/moin/AskForHelp
---
您收到此邮件是因为您订阅了Google网上论坛上的“python-cn(华蟒用户组,CPyUG 邮件列表)”群组。
要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到python-cn+...@googlegroups.com
要发帖到此群组,请发送电子邮件至pyth...@googlegroups.com
要查看更多选项,请访问https://groups.google.com/d/optout


--

Shell Xu

unread,
Jan 15, 2019, 2:26:22 AM1/15/19
to CUPG
这是搞不定级联的,要用lazy对象。

piglei

unread,
Jan 15, 2019, 5:01:36 AM1/15/19
to pyth...@googlegroups.com
乱写了一个感觉很多 bug 的版本:

# -*- coding: utf-8 -*- class MyDict(dict): def __setattr__(self, key, value): self[key] = value def __getattr__(self, key): try: return self[key] except KeyError: return self.setdefault(key, MyDict()) mydict = MyDict() mydict.a = 1 mydict.b = 2 mydict.c.d = 3 print mydict print mydict.c

我对这种题目倒不至于那么讨厌,感觉可以考,但不应该被当做评估候选人能力的重点。

YS.Zou

unread,
Jan 15, 2019, 5:37:48 AM1/15/19
to pyth...@googlegroups.com
考那些特殊方法的吧。
是我就把“继承dict类”去掉,顺便 __repr__  __str__ 也考了。

kougazhang <kouga...@gmail.com> 于2019年1月15日周二 下午1:19写道:
--
邮件来自: `CPyUG`华蟒用户组(中文Python技术邮件列表)
规则: http://code.google.com/p/cpyug/wiki/PythonCn
详情: http://code.google.com/p/cpyug/wiki/CpyUg
严正: 理解列表! 智慧提问! http://wiki.woodpecker.org.cn/moin/AskForHelp
---
您收到此邮件是因为您订阅了Google网上论坛上的“python-cn(华蟒用户组,CPyUG 邮件列表)”群组。
要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到python-cn+...@googlegroups.com
要发帖到此群组,请发送电子邮件至pyth...@googlegroups.com
要查看更多选项,请访问https://groups.google.com/d/optout


--
进出自由才是游戏者的生存之道。

http://www.zouyesheng.com

Leo Xu

unread,
Jan 15, 2019, 6:52:14 AM1/15/19
to pyth...@googlegroups.com
又是面试放卫星,上班揉面筋。
这些写法要真整一堆到线上环境。。。
面试官成天关注这些刁钻的用法,不如踏实点问问系统优化思路,Bug排查方式,踩过哪些坑。
就算问问能不能玩到一起也比这靠谱啊。
单纯吐槽下现象,不针对。

YS.Zou <yeshe...@gmail.com>于2019年1月15日 周二18:37写道:
--
凌川

jonson Chou

unread,
Jan 15, 2019, 7:35:41 AM1/15/19
to pyth...@googlegroups.com
哈哈哈, 面试放卫星,上班揉面筋 还挺押韵的

Leo Xu <tvb...@gmail.com> 于2019年1月15日周二 下午7:52写道:

YS.Zou

unread,
Jan 15, 2019, 9:49:26 AM1/15/19
to pyth...@googlegroups.com
了解不了解,与现实中用不用,又不是一回事。
而且,以这题来说,完全还不到“刁难”的程度吧。


Leo Xu <tvb...@gmail.com> 于2019年1月15日周二 下午7:52写道:
又是面试放卫星,上班揉面筋。


--
进出自由才是游戏者的生存之道。

http://www.zouyesheng.com

Shell Xu

unread,
Jan 15, 2019, 12:56:50 PM1/15/19
to CUPG
这题其实难度不高,但是挺繁琐。平时把玩一下觉得挺好玩,但是真的用来考察人,容易陷入到奇技淫巧的范畴里。
有位朋友说过,面试题最怕出成。不知道某个知识点难似登天,知道之后没有难度的题。我私下还加一句,这个知识还不是日常必备知识。

fy

unread,
Jan 15, 2019, 8:38:19 PM1/15/19
to pyth...@googlegroups.com
好久没看到py2的代码了。

class JsDict(dict):
    def __getitem__(self, item):
        return self.get(item)

    def __repr__(self):
        return '<jsDict ' + dict.__repr__(self) + '>'

    __setattr__ = dict.__setitem__
    __delattr__ = dict.__delitem__
    __getattr__ = __getitem__


还需要重载一下赋值将value为dict类型的转换为JsDict类型。


Shell Xu <shell...@gmail.com> 于2019年1月16日周三 上午1:56写道:


--
我的github: http://github.com/fy0



yu da

unread,
Jan 17, 2019, 6:05:38 AM1/17/19
to pyth...@googlegroups.com
感觉这考的是,类的静态方法。

寒士

unread,
Jan 24, 2019, 11:05:26 PM1/24/19
to pyth...@googlegroups.com
哪就押韵了,怕不是你对押韵有什么误解。

¥is!


jonson Chou <fly...@gmail.com> 于2019年1月15日周二 下午8:35写道:

yu da

unread,
Feb 8, 2019, 5:46:08 AM2/8/19
to pyth...@googlegroups.com
感觉这考的是,类的静态方法。

在 2019年1月15日,下午10:48,YS.Zou <yeshe...@gmail.com> 写道:

刘玉龙

unread,
Feb 12, 2019, 1:36:04 AM2/12/19
to python-cn(华蟒用户组,CPyUG 邮件列表)
补了一个逻辑:某个变量是dict时,也让它能用,不过感觉这样写好丑

class MyDict(dict):

   
def __setattr__(self, key, value):
       
self[key] = value

   
def __getattr__(self, key):
       
try:

            value
= self[key]
           
if isinstance(value, dict) and not isinstance(value, MyDict):
               
self[key] = MyDict(value)
                value
= self[key]
       
except KeyError:
            value
= self.setdefault(key, MyDict())
       
return value

mydict
= MyDict()

mydict
.a = 1
mydict.b = 2
mydict.c.d = 3
mydict.e = {'f': 4}

print(mydict)
print(mydict.c)
print(mydict.e.f)


在 2019年1月15日星期二 UTC+8下午6:01:36,piglei写道:
这是搞不定级联的,要用lazy对象。

要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到python-cn+unsubscribe@googlegroups.com
要发帖到此群组,请发送电子邮件至python-cn@googlegroups.com
要查看更多选项,请访问https://groups.google.com/d/optout


--

--
邮件来自: `CPyUG`华蟒用户组(中文Python技术邮件列表)
规则: http://code.google.com/p/cpyug/wiki/PythonCn
详情: http://code.google.com/p/cpyug/wiki/CpyUg
严正: 理解列表! 智慧提问! http://wiki.woodpecker.org.cn/moin/AskForHelp
---
您收到此邮件是因为您订阅了Google网上论坛上的“python-cn(华蟒用户组,CPyUG 邮件列表)”群组。
要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到python-cn+unsubscribe@googlegroups.com
要发帖到此群组,请发送电子邮件至python-cn@googlegroups.com
要查看更多选项,请访问https://groups.google.com/d/optout


--
彼節者有間,而刀刃者無厚;以無厚入有間,恢恢乎其於游刃必有餘地矣。
blog: http://shell909090.org/

--
邮件来自: `CPyUG`华蟒用户组(中文Python技术邮件列表)
规则: http://code.google.com/p/cpyug/wiki/PythonCn
详情: http://code.google.com/p/cpyug/wiki/CpyUg
严正: 理解列表! 智慧提问! http://wiki.woodpecker.org.cn/moin/AskForHelp
---
您收到此邮件是因为您订阅了Google网上论坛上的“python-cn(华蟒用户组,CPyUG 邮件列表)”群组。
要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到python-cn+unsubscribe@googlegroups.com
要发帖到此群组,请发送电子邮件至python-cn@googlegroups.com
要查看更多选项,请访问https://groups.google.com/d/optout

Freedom anana

unread,
Mar 6, 2019, 3:02:38 AM3/6/19
to python-cn(华蟒用户组,CPyUG 邮件列表)
getattr做一个关键字检查不就有了.好像有一个内置的iskeyword()函数.

Freedom anana

unread,
Mar 6, 2019, 4:50:42 AM3/6/19
to python-cn(华蟒用户组,CPyUG 邮件列表)
from collections import UserDict
class Mydict(dict):
    def __setattr__(self,key,value):
        self[key] = value


    def __getattr__(self,key):
        try:
            return self[key]
        except:
            self[key] = Mydict()
            return self[key]



mydict = Mydict()

mydict.a = 1
mydict.b = 2
mydict.c.d = 4
mydict.e.f.g = 5

print(mydict)
print(mydict.c)
print(mydict.c.d)
print(mydict.e,mydict.e.f,mydict.e.f.g)

{'a': 1, 'b': 2, 'c': {'d': 4}, 'e': {'f': {'g': 5}}}
{'d': 4}
4
{'f': {'g': 5}} {'g': 5} 5
[Finished in 0.063s]

Freedom anana

unread,
Mar 6, 2019, 5:00:46 AM3/6/19
to python-cn(华蟒用户组,CPyUG 邮件列表)
class Mydict(dict):

    def __getattr__(self,key):
        try:
            return self.__dict__[key]
        except:
            self.__dict__[key] = Mydict()
            return self.__dict__[key]

    def __repr__(self):
        return repr(self.__dict__)



mydict = Mydict()

mydict.a = 1
mydict.b = 2
mydict.c.d = 3

print(mydict)
print(mydict.c)
print(mydict.c.d)
{'a': 1, 'b': 2, 'c': {'d': 3}}
{'d': 3}
3
[Finished in 0.091s]


On Tuesday, January 15, 2019 at 1:18:44 PM UTC+8, kougazhang wrote:

Oliver

unread,
Mar 11, 2019, 8:45:23 AM3/11/19
to python-cn(华蟒用户组,CPyUG 邮件列表)
太多方法了,property也可以,或者用descriptor也可以。

在 2019年1月15日星期二 UTC+8下午1:18:44,kougazhang写道:
Reply all
Reply to author
Forward
0 new messages