推荐学习书目
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 · Nov 28, 2016 · 3357 views
    This topic created in 3465 days ago, the information mentioned may be changed or developed.

    下面的代码我的一个 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 replies    2016-11-28 14:49:45 +08:00
    hugo775128583
        1
    hugo775128583  
       Nov 28, 2016
    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  
       Nov 28, 2016
    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
       Nov 28, 2016
    @hugo775128583 无效,用 return object.__new__(cls) 的话参数不同基本上就是 __init__ 无法接收这些参数,一旦在 return object.__new__(cls) 时添加更多的参数,就会报 object() takes no parameters
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3516 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 42ms · UTC 11:16 · PVG 19:16 · LAX 04:16 · JFK 07:16
    ♥ Do have faith in what you're doing.