+ 3

What are the rules of indentation for classes and inheritance?

Do the child classes get indented?

18th Nov 2018, 2:42 PM
Thomas Knight
Thomas Knight - avatar
10 ответов
+ 4
Yes, in line 2, you have a "def __init__(self):", and then suddenly a definition of a new class! It was expecting something to be written in the function. For now, you can just put "pass" in line 3, and the code would work fine. Later you can add stuff there. Having an empty parent class doesn't make much sense. (Nothing to inherit? The children would not be happy. 😉)
18th Nov 2018, 4:09 PM
Kishalaya Saha
Kishalaya Saha - avatar
+ 3
No, a child class has the same indentation style as a regular class. Check out the examples here: https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2469/
18th Nov 2018, 3:10 PM
Kishalaya Saha
Kishalaya Saha - avatar
+ 3
Thomas Knight your code is right indent the problem is that you define a class gameobject which is initialize using init method but you don't give him any attributes that's why with child class indent problem occur. so you need to use pass statement means you don't give him any attributes.pass means empty. Check this code. https://code.sololearn.com/cgQ1EnZ54n9w/?ref=app
18th Nov 2018, 4:11 PM
Maninder $ingh
Maninder $ingh - avatar
+ 3
Thanks. I updated it to say self.name = name on line 3 and it works. I dont really need to have it where i can call name on Game_object yet but maybe later for trash items you can collect.
18th Nov 2018, 4:13 PM
Thomas Knight
Thomas Knight - avatar
+ 3
Great! If you think you'll never need it, you could also remove the parent class altogether. I'm not an expert on classes (and I've never played DnD), but people tend to use a parent class to put all the stuff that are common to the children in one place, instead of writing them for every child.
18th Nov 2018, 4:27 PM
Kishalaya Saha
Kishalaya Saha - avatar
+ 3
Child (Inherited) classes aren't indented in python but Inner classes are.
18th Nov 2018, 6:31 PM
Mayur Garg
Mayur Garg - avatar
+ 2
Always remember in python when you use colon end of the line then next line will be indent.you can indent your code with the help of tab button. e.g If(x==3): print('hi') #after if you use colon means next line will be indent. And you don't need to indent the child class in python.
18th Nov 2018, 3:58 PM
Maninder $ingh
Maninder $ingh - avatar
+ 2
Ok . Maybe im doing something wrong. https://code.sololearn.com/cw7DSwb3ZkTs/?ref=app I keep getting unexpected or expected indent errors that are confusing me
18th Nov 2018, 4:03 PM
Thomas Knight
Thomas Knight - avatar
+ 2
I think because there is no def func() in the parent class and it ends on : ?
18th Nov 2018, 4:05 PM
Thomas Knight
Thomas Knight - avatar
+ 2
I believe the parent class will work for basically any items in game that dont have a lot of meaningful attributes in what I'm trying to accomplish. And honestly i have no idea if I'm even remotely close to what I'm trying to do. It's my third script and first time using classes. Thanks for the input guys! I could probably have stayed on something a bit more basic but dnd is something im familiar enough with and passionate enough about
18th Nov 2018, 6:37 PM
Thomas Knight
Thomas Knight - avatar