class-design

Using a class' __new__ method as a Factory: __init__ gets called twice

Using a class' __new__ method as a Factory: __init__ gets called twice Question: I encountered a strange bug in python where using the __new__ method of a class as a factory would lead to the __init__ method of the instantiated class to be called twice. The idea was originally to use the __new__ method of …

Total answers: 3

How do I design a class in Python?

How do I design a class in Python? Question: I’ve had some really awesome help on my previous questions for detecting paws and toes within a paw, but all these solutions only work for one measurement at a time. Now I have data that consists off: about 30 dogs; each has 24 measurements (divided into …

Total answers: 6

python circular imports once again (aka what's wrong with this design)

python circular imports once again (aka what's wrong with this design) Question: Let’s consider python (3.x) scripts: main.py: from test.team import team from test.user import user if __name__ == ‘__main__’: u = user() t = team() u.setTeam(t) t.setLeader(u) test/user.py: from test.team import team class user: def setTeam(self, t): if issubclass(t, team.__class__): self.team = t test/team.py: …

Total answers: 5

Why is __init__() always called after __new__()?

Why is __init__() always called after __new__()? Question: I’m just trying to streamline one of my classes and have introduced some functionality in the same style as the flyweight design pattern. However, I’m a bit confused as to why __init__ is always called after __new__. I wasn’t expecting this. Can anyone tell me why this …

Total answers: 20