Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![Jeff Hartl](https://uploads.teamtreehouse.com/production/profile-photos/1465872/micro_150px-Triangulated_truncated_octahedron.png)
Jeff Hartl
Python Web Development Techdegree Student 8,420 PointsWhy not put "def __init__" before other methods in character.py?
In Kenneth's code for the Character class (character.py), he put the def __init__
method below the methods attack()
and get_weapon(self)
. This looks funny to me -- funny/strange, not funny/ha-ha (highly technical phrasing, I know).
If I understand correctly, when a instance of a class is created, the order in which the methods were written in the underlying .py file isn't always crucial. However, wouldn't this particular code look cleaner and be easier to read if def __init__(self, **kwargs)
were the first method (after any defaults are declared, of course) that we come across in the Character class definition?
In general, when writing a class, shouldn't the__init__
method come before any other methods within the class? Not for the sake of functionality, but for readability?
3 Answers
![Steven Parker](https://uploads.teamtreehouse.com/production/profile-photos/969492/micro_tool_chess_set_2.jpg)
Steven Parker
230,688 PointsIt makes sense to me also, but I checked the PEP8 and didn't find it in there. It did say that module-level "dunder" names should come before others, and before imports. But no mention of method-level names.
So I guess it's not generally considered that important. Seems like it would be a good practice though.
![Shailesh Gupta](https://secure.gravatar.com/avatar/eb3ed152e53e44c8280e4ec68f312b54?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Shailesh Gupta
12,885 PointsIt doesn't matter where we put our methods in the class as long as it doesn't cause errors. As python is interpreted language (code is executed line by line). So, the methods which call another method must come after. For example:
def A(): #some code def B(): A() #some code
Thus, if B() is calling A(), so it must come after A(). So is the case with all the method calling. It doesn't matter where we put int() methods (magical methods) as long as they obey calling procedures.
I hope this solves the issue.
![Andre Merten](https://uploads.teamtreehouse.com/production/profile-photos/8124432/micro_spaceman.jpg)
Andre Merten
9,856 Points@ Jeff Hartl. I too had this question and concern. I think it is good practice to always define init(self): as the first method after creating a Class. I usually think of it as the 'Boot up sequence' if you will. While apparently not necessary to be first, I think it does have a much cleaner and neater look or "Readability", like you said. Great question Jeff.