diff --git a/README.md b/README.md index 6dfa220..51b31cb 100644 --- a/README.md +++ b/README.md @@ -731,7 +731,7 @@ Then we will store the counter of the number of registration as a class attribut ``` #[classattr] -fn reg_num() -> u32 { +fn cur_reg_num() -> u32 { 0 } ``` @@ -745,11 +745,11 @@ fn new(cls: &Bound<'_, PyType>, name: String, speaker: bool) -> PyResult { if name.len() == 0 { Err(PyValueError::new_err("Please enter a name")) } else { - let cur_num: u32 = cls.getattr("reg_num")?.extract()?; - cls.setattr("reg_num", cur_num + 1)?; + let cur_reg_num: u32 = cls.getattr("cur_reg_num")?.extract()?; + cls.setattr("cur_reg_num", cur_reg_num + 1)?; Ok( Attendee{ - reg_num: cur_num, + reg_num: cur_reg_num, name: name, speaker: speaker, } @@ -758,7 +758,7 @@ fn new(cls: &Bound<'_, PyType>, name: String, speaker: bool) -> PyResult { } ``` -As a class method, the first argument is a pointer to the Python class `Attendee` itself. We can then use `getattr` to get back the value of the attribute `reg_num` and then convert it to a Rust integer using `extract`. Both methods will return a `Result` there for we add the `?` to get back the value if `Ok`. We then use `setattr` to increate the class attribute by 1 and then add the number to our new attendee created. +As a class method, the first argument is a pointer to the Python class `Attendee` itself. We can then use `getattr` to get back the value of the attribute `cur_reg_num` and then convert it to a Rust integer using `extract`. Both methods will return a `Result` therefore we add the `?` to get back the value if `Ok`. We then use `setattr` to increate the class attribute by 1 and then add the number to our new attendee created. Also, don't forget to add this: ``` @@ -771,21 +771,21 @@ Now, lets test it out to see if it works as expected: ``` # test Attendee -print(f"Number of attendees are {p1.Attendee.reg_num}") +print(f"Number of attendees are {p1.Attendee.cur_reg_num}") me = p1.Attendee('Cheuk', True) print(me.name, me.speaker, me.reg_num) -print(f"Number of attendees are {p1.Attendee.reg_num}") +print(f"Number of attendees are {p1.Attendee.cur_reg_num}") keynote = p1.Attendee('John', True) print(keynote.name, keynote.speaker, keynote.reg_num) keynote.name = 'Jon' print(keynote.name, keynote.speaker, keynote.reg_num) -print(f"Number of attendees are {p1.Attendee.reg_num}") +print(f"Number of attendees are {p1.Attendee.cur_reg_num}") ``` -You may found it strange why the first attendee created will have `reg_num` 1 instead of 0. I suspect this has something to do with Python interpret the code at run time while Rust is compiled before hand and the class we were referencing is a Python object... this will goes into a rabbit hole of what's happening behind the hood, so let's park this mystery for now and move on to the next exercise. But if you have time, feel free to play with it some more and look into this. +Feel free to create another class attribute and class methods before moving to the next exercise. --- diff --git a/ex_6/lib.rs b/ex_6/lib.rs index 16b40f1..4933bb6 100644 --- a/ex_6/lib.rs +++ b/ex_6/lib.rs @@ -71,7 +71,7 @@ struct Attendee { #[pymethods] impl Attendee { #[classattr] - fn reg_num() -> u32 { + fn cur_reg_num() -> u32 { 0 } #[new] @@ -80,11 +80,11 @@ impl Attendee { if name.len() == 0 { Err(PyValueError::new_err("Please enter a name")) } else { - let cur_num: u32 = cls.getattr("reg_num")?.extract()?; - cls.setattr("reg_num", cur_num + 1)?; + let cur_reg_num: u32 = cls.getattr("cur_reg_num")?.extract()?; + cls.setattr("cur_reg_num", cur_reg_num + 1)?; Ok( Attendee{ - reg_num: cur_num, + reg_num: cur_reg_num, name: name, speaker: speaker, } diff --git a/ex_6/try.py b/ex_6/try.py index f458928..f056a9b 100644 --- a/ex_6/try.py +++ b/ex_6/try.py @@ -22,18 +22,18 @@ print(p1.travel_avg(budget_dict)) # test Attendee -print(f"Number of attendees are {p1.Attendee.reg_num}") +print(f"Number of attendees are {p1.Attendee.cur_reg_num}") me = p1.Attendee('Cheuk', True) print(me.name, me.speaker, me.reg_num) -print(f"Number of attendees are {p1.Attendee.reg_num}") +print(f"Number of attendees are {p1.Attendee.cur_reg_num}") keynote = p1.Attendee('John', True) print(keynote.name, keynote.speaker, keynote.reg_num) keynote.name = 'Jon' print(keynote.name, keynote.speaker, keynote.reg_num) -print(f"Number of attendees are {p1.Attendee.reg_num}") +print(f"Number of attendees are {p1.Attendee.cur_reg_num}") # test Fibonacci # fib = p1.Fibonacci() diff --git a/ex_7/lib.rs b/ex_7/lib.rs index 1110fa9..245a7a2 100644 --- a/ex_7/lib.rs +++ b/ex_7/lib.rs @@ -73,7 +73,7 @@ struct Attendee { #[pymethods] impl Attendee { #[classattr] - fn reg_num() -> u32 { + fn cur_reg_num() -> u32 { 0 } #[new] @@ -82,11 +82,11 @@ impl Attendee { if name.len() == 0 { Err(PyValueError::new_err("Please enter a name")) } else { - let cur_num: u32 = cls.getattr("reg_num")?.extract()?; - cls.setattr("reg_num", cur_num + 1)?; + let cur_reg_num: u32 = cls.getattr("cur_reg_num")?.extract()?; + cls.setattr("cur_reg_num", cur_reg_num + 1)?; Ok( Attendee{ - reg_num: cur_num, + reg_num: cur_reg_num, name: name, speaker: speaker, } diff --git a/ex_7/try.py b/ex_7/try.py index d52578d..5dff295 100644 --- a/ex_7/try.py +++ b/ex_7/try.py @@ -22,18 +22,18 @@ print(p1.travel_avg(budget_dict)) # test Attendee -print(f"Number of attendees are {p1.Attendee.reg_num}") +print(f"Number of attendees are {p1.Attendee.cur_reg_num}") me = p1.Attendee('Cheuk', True) print(me.name, me.speaker, me.reg_num) -print(f"Number of attendees are {p1.Attendee.reg_num}") +print(f"Number of attendees are {p1.Attendee.cur_reg_num}") keynote = p1.Attendee('John', True) print(keynote.name, keynote.speaker, keynote.reg_num) keynote.name = 'Jon' print(keynote.name, keynote.speaker, keynote.reg_num) -print(f"Number of attendees are {p1.Attendee.reg_num}") +print(f"Number of attendees are {p1.Attendee.cur_reg_num}") # test Fibonacci # fib = p1.Fibonacci()