Skip to content

refactor: Replace h.get_type() with py::type(h) #2388

Closed
@henryiii

Description

@henryiii

With the addition of #2364, a very natural extension to py::type<T>() would be py::type(h), mimicking py::cast. I would propose we depreciate and eventually remove h.get_type() in favor of py::type(h). Reasons:

  • Free functions are generally better than member functions in C++; they are simpler, composable, etc.
  • It would simplify the mixin, which currently gets added to several different classes, and keeps the API closer to Python.
  • h.str() is already deprecated in favor of py:str(h).

It is only used internally, not publicly in any docs or tests, so to me it seems it would read better (more python like) and simplify the mixin.

For example, randomly picking one of the 10 places it's currently used:

if (!obj.get_type().is(matrix_type))
// Becomes
if (!py::type(obj).is(matrix_type))

For comparison, the Python would be:

if not type(obj) is matrix_type:

(Discussion moved from #2364).

Metadata

Metadata

Assignees

Labels

policySuggestions for changes in policy

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions