Skip to content

Commit

Permalink
Add a hint on using intern! to Py{,Any}::{set,get}attr.
Browse files Browse the repository at this point in the history
  • Loading branch information
adamreichold committed Apr 4, 2022
1 parent 2c95b3a commit 05f0123
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,9 @@ impl<T> Py<T> {
/// Retrieves an attribute value.
///
/// This is equivalent to the Python expression `self.attr_name`.
///
/// If calling this method becomes performance-critical, using the [`intern!`] macro can be
/// used to intern `attr_name` thereby avoid repeated temporary allocations of Python strings.
pub fn getattr<N>(&self, py: Python<'_>, attr_name: N) -> PyResult<PyObject>
where
N: ToPyObject,
Expand All @@ -546,6 +549,9 @@ impl<T> Py<T> {
/// Sets an attribute value.
///
/// This is equivalent to the Python expression `self.attr_name = value`.
///
/// If calling this method becomes performance-critical, using the [`intern!`] macro can be
/// used to intern `attr_name` thereby avoid repeated temporary allocations of Python strings.
pub fn setattr<N, V>(&self, py: Python<'_>, attr_name: N, value: V) -> PyResult<()>
where
N: ToPyObject,
Expand Down
6 changes: 6 additions & 0 deletions src/types/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ impl PyAny {
/// Retrieves an attribute value.
///
/// This is equivalent to the Python expression `self.attr_name`.
///
/// If calling this method becomes performance-critical, using the [`intern!`] macro can be
/// used to intern `attr_name` thereby avoid repeated temporary allocations of Python strings.
pub fn getattr<N>(&self, attr_name: N) -> PyResult<&PyAny>
where
N: ToPyObject,
Expand All @@ -124,6 +127,9 @@ impl PyAny {
/// Sets an attribute value.
///
/// This is equivalent to the Python expression `self.attr_name = value`.
///
/// If calling this method becomes performance-critical, using the [`intern!`] macro can be
/// used to intern `attr_name` thereby avoid repeated temporary allocations of Python strings.
pub fn setattr<N, V>(&self, attr_name: N, value: V) -> PyResult<()>
where
N: ToBorrowedObject,
Expand Down

0 comments on commit 05f0123

Please sign in to comment.