-
Notifications
You must be signed in to change notification settings - Fork 801
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
docs: show pattern for self.__class__.__name__ in __repr__ #3067
Conversation
Thank you for sharing your findings! I agree that it is not obvious how to approach this. However, I would suggest not burdening this particular example with the complication but to start a new example on how to access the class name. Concerning the implementation, instead of starting with diff --git a/guide/src/class/object.md b/guide/src/class/object.md
index 357fdcd7..5b099ed3 100644
--- a/guide/src/class/object.md
+++ b/guide/src/class/object.md
@@ -53,13 +53,17 @@ contained inside `Number`.
impl Number {
// For `__repr__` we want to return a string that Python code could use to recreate
// the `Number`, like `Number(5)` for example.
- fn __repr__(&self) -> String {
+ fn __repr__(self_: &PyCell<Self>, py: Python<'_>) -> PyResult<String> {
+ let class_name: String = self_
+ .getattr("__class__")?
+ .getattr("__name__")?
+ .extract()?;
// We use the `format!` macro to create a string. Its first argument is a
// format string, followed by any number of parameters which replace the
// `{}`'s in the format string.
//
// 👇 Tuple field access in Rust uses a dot
- format!("Number({})", self.0)
+ Ok(format!("{class_name}({})", self_.borrow().0))
}
// `__str__` is generally used to create an "informal" representation, so we However, for this particular issues (getting the name of Rust-implemented Python class), there is simpler way IIUC via our own diff --git a/guide/src/class/object.md b/guide/src/class/object.md
index 357fdcd7..c2d0865f 100644
--- a/guide/src/class/object.md
+++ b/guide/src/class/object.md
@@ -44,7 +44,7 @@ It can't even print an user-readable representation of itself! We can fix that b
contained inside `Number`.
```rust
-# use pyo3::prelude::*;
+# use pyo3::{prelude::*, PyTypeInfo};
#
# #[pyclass]
# struct Number(i32);
@@ -59,7 +59,7 @@ impl Number {
// `{}`'s in the format string.
//
// 👇 Tuple field access in Rust uses a dot
- format!("Number({})", self.0)
+ format!("{}({})", Self::NAME, self.0)
}
// `__str__` is generally used to create an "informal" representation, so we which I would probably prefer to show case. But I am not sure whether we want to guide user's towards these "infrastructure traits" too strongly? |
The second one is also incorrect in subclasses. But couldn't the lookup of class and name be done faster with native APIs? |
Indeed! But you'd still have to go via |
I would be in favour of not doing so, I have been pondering a move of (While |
Ok, so I think the consensus so far is:
|
Thanks for the advice. Btw #1059 would still be really useful; I had no idea you could take |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, let's give it a while for others to have a look and if there are no objections we can merge. Thanks again for taking the time to improve the docs!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, just a small nit
@wjones127 Could you squash your commits before merging? Thanks. |
bors r+ |
3067: docs: show pattern for self.__class__.__name__ in __repr__ r=adamreichold a=wjones127 It took me a little while to figure out this pattern in PyO3, so I thought it would be a good addition to the guide. It's not the cleanest pattern, so would welcome suggestions on how to make it shorter or easier. Co-authored-by: Will Jones <[email protected]>
Build failed: |
Co-authored-by: Bruno Kolenbrander <[email protected]>
I force-pushed to remove the usage of inline format arguments. bors r+ |
Build succeeded: |
It took me a little while to figure out this pattern in PyO3, so I thought it would be a good addition to the guide.
It's not the cleanest pattern, so would welcome suggestions on how to make it shorter or easier.