Oct-20-2021, 05:13 PM
(Oct-20-2021, 01:08 PM)muzikman Wrote: This guy is great. So, when designing a class, what things make you consider using name mangling? I am thinking that it mangles the name in order to prevent name collision, maybe? We can still access the variable even after we mangle it?If look at my last example so do sub-class
Bar
use same name __var = 3
,normally would this override the firs one.Name mangling is helpful for letting subclasses override methods without breaking intraclass method calls.
Let say a new company say a car is newer than is bye override method
year_made
.I have not find to much usage for this yet.
class CarMaker: # car_id is not meant to be a part of the public interface # So it's a advice that is best to leave it alone _car_id = 12345 def __init__(self, color): self.car_color = color def year_made(self): return 2019 __year_made = year_made # Private copy of original year_made() method class NewCompany(CarMaker): def year_made(self): return 2020Use:
>>> car = NewCompany('Red') >>> car.year_made() 2020 >>> # Carmaker still have orginal year made >>> car._CarMaker__year_made() 2019
(Oct-20-2021, 01:08 PM)muzikman Wrote: I've never heard of the __slots__ dunder. When should it be used?It's for memory saving and faster attribute access,look at link.
(Oct-20-2021, 01:08 PM)muzikman Wrote: You answer almost all of the questions on this site. You must be a Python master.No it's just hobby that i used some(or lot time in periods) time over many years.