V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
mgna17
V2EX  ›  Python

python3.3 以上版本的新式类中,如何在同时覆盖 __new__ 和 __init__ 时,将自定义的额外参数从 __new__ 传入 __init__ ?

  •  1
     
  •   mgna17 · 2016-11-28 13:56:23 +08:00 · 2820 次点击
    这是一个创建于 2709 天前的主题,其中的信息可能已经有所发展或是发生改变。

    下面的代码我的一个 class ,如何将一些自定义参数在__new__中 return object.__new__(cls) 的时候传入 __init__ 呢?

    比如,我在写 python2 的时候可以直接 return object.__new__(cls, arg0, arg1, **kwargs), python3.3+就不能直接这样写了。

    python3.3 以上的版本在 return object.__new__(cls) 时直接传入自定义参数的时候会报出 TypeError: object() takes no parameters 。

    参考了:StackOverflow 上的帖子

    from flask import current_app
    
    from utils import SHA256
    from orm.user import User
    
    
    class UserInfo(object):
        ''' 
        用户基本信息修改类功能
        '''
    
        def __new__(cls, **kwargs):
            userid = kwargs.get('id')
            name = kwargs.get('name')
            uid_list = current_app.config.get('USER_ID_LIST')
            uname_list = current_app.config.get('USER_NAME_LIST')
            if userid in uid_list or name in uname_list:
                return object.__new__(cls)
            return None
    
        def __init__(self, **kwargs):
            uid = kwargs.get('id')
            name = kwargs.get('name')
    
            if uid:
                obj = User.query.filter_by(id=uid).first()
            elif name:
                obj = User.query.filter_by(name=name).first()
            self.obj = obj 
            self.user_id = obj.id
            self.user_name = obj.name
    
        def set_pwd(self, pwd):
            self.obj.passwd = SHA256(pwd)
            self.obj.update()
    
    3 条回复    2016-11-28 14:49:45 +08:00
    hugo775128583
        1
    hugo775128583  
       2016-11-28 14:19:41 +08:00
    class A(object):
    def __new__(cls, i, j):
    print(cls, i, j)
    return object.__new__(cls)

    def __init__(self, x, y):
    print(self, x, y)
    self.x = x
    self.y = y

    Python3 中 __new__ 和 __init__ 方法接受的参数必须是一样的。

    lz 可以试试参数不一样的情况。
    hugo775128583
        2
    hugo775128583  
       2016-11-28 14:21:32 +08:00
    format 一蛤

    ```python
    class A(object):
    def __new__(cls, i, j):
    print(cls, i, j)
    return object.__new__(cls)

    def __init__(self, x, y):
    print(self, x, y)
    self.x = x
    self.y = y
    ```

    Python3 中 __new__ 和 __init__ 方法接受的参数必须是一样的。

    lz 可以试试参数不一样的情况
    mgna17
        3
    mgna17  
    OP
       2016-11-28 14:49:45 +08:00
    @hugo775128583 无效,用 return object.__new__(cls) 的话参数不同基本上就是 __init__ 无法接收这些参数,一旦在 return object.__new__(cls) 时添加更多的参数,就会报 object() takes no parameters
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   946 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 21:35 · PVG 05:35 · LAX 14:35 · JFK 17:35
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.