Skip to content

API: use __new__ in .create to remove __init__ totally #1149

Description

@Zeroto521

Is your feature request related to a problem? Please describe.

__init__ method is not used in PySCIPOpt due to the SCIP pointer.
So create comes out to instead of __init__.
And it could still see __init__ in .create.

The chain to generate an instance via __init__.

  1. Run __new__ and return a empty instance
  2. Run __init__ and return this instance.

Describe the solution you'd like

use __new__ to generate a instance. It could avoid running any code in __init__.
And a little speed up via skipping the checking.

cdef class Solution:

    def __init__(self):
        raise ValueError

    @staticmethod
    cdef create(SCIP* scip, SCIP_SOL* sol):
        if scip == NULL:
            raise Warning("cannot create Solution with SCIP* == NULL")

        cdef Solution res = Solution.__new__(Solution)
        res.sol = sol
        res.scip = scip
        return res

use __init__ to generate a instance

cdef class Solution:

    def __init__(self, raise_error = False):
        if not raise_error:
            raise ValueError

    @staticmethod
    cdef create(SCIP* scip, SCIP_SOL* scip_sol):
        if scip == NULL:
            raise Warning("cannot create Solution with SCIP* == NULL")
        sol = Solution(True)
        sol.sol = scip_sol
        sol.scip = scip
        return sol

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions