Skip to content

Get a pyclass from a pyclass with reference? #2261

Answered by adamreichold
sun-rs asked this question in Questions
Discussion options

You must be logged in to vote
#[pyclass]
struct Wallet {
    pub cash: i64,
}

#[pymethods]
impl Wallet {
    #[new]
    fn new(cash: i64) -> Self {
        Self { cash }
    }
    pub fn add_cash(&mut self, cash: i64) {
        self.cash += cash;
    }
}

#[pyclass]
struct Player {
    pub name: String,
    #[pyo3(get)]
    pub wallet: Py<Wallet>,
}

#[pymethods]
impl Player {
    #[new]
    fn new(name: String, cash: i64, py: Python) -> Self {
        Self {
            name,
            wallet: Py::new(py, Wallet { cash }).unwrap(),
        }
    }
}

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@sun-rs
Comment options

@adamreichold
Comment options

@sun-rs
Comment options

@adamreichold
Comment options

Answer selected by sun-rs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants