+ 2

SoloLearn's lesson misunderstanding

SoloLearn's lesson misunderstanding Code: class GameObject: class_name = "" desc = "" objects = {} def __init__(self, name): self.name = name GameObject.objects[self.class_name] = self def get_desc(self): return self.class_name + "\n" + self.desc class Goblin(GameObject): class_name = "goblin" desc = "A foul creature" goblin = Goblin("Gobbly") def examine(noun): if noun in GameObject.objects: return GameObject.objects[noun].get_desc() else: return "There is no {} here.".format(noun) Question: I don't understand why first line with "GameObject.objects..." in this code was written without any tabs? If it's in the definition of class GameObject it should be written with one tab before the line code, but it was written as something after the class definition. It will be maybe okay, but after that is "def get_desc" with one tab, so get_desc is in class.

21st Aug 2018, 5:06 PM
Ilyich
10 odpowiedzi
+ 1
Ilyich when I copy the code above and paste it in Code Playground editor I see that 'GameObjects.objects' line is indeed indented within the 'def __init__' block, so that line belongs to the init definition block
22nd Aug 2018, 9:01 AM
Ipang
+ 2
Ipang you have 2 spaces before "GameObject.objects" or you mean empty line before "GameObject.objects"?
22nd Aug 2018, 10:03 AM
Ilyich
+ 2
Ilyich the 'GameObject.objects' line is at the same indent level with 'self.name = name' both lines are within init def.
22nd Aug 2018, 10:06 AM
Ipang
+ 2
Ilyich copy the code only, and paste in Code Playground editor to see what I mean : )
22nd Aug 2018, 10:13 AM
Ipang
+ 1
idk what you mean by tabs, but i am assuming you mean indentation to define scope in python. Python dosent need tab for indentation, even spaces would do, rather you should always use spaces according to pep8. Now coming to your question, i can see perfectly fine indentation here, the class is defined without any spaces so its the opening of code, after that inside the class scope, is the first function __init__, which needs to have its own indentation, in which GameObject.objects is written, so its inside the __init__ function which in turn is inside the class definition, same goes for get_desc. Hope this helped you
22nd Aug 2018, 2:04 AM
Mayank
Mayank - avatar
+ 1
But there isn't any space before GameObject.objects line of code
22nd Aug 2018, 8:57 AM
Ilyich
+ 1
Yes Ilyich what is it? you called for me?
22nd Aug 2018, 1:12 PM
Ipang
+ 1
Ipang it is code with GameObject problem. You said there are spaces before this line of code, but when I copy code, there isn't it. But, thanks, I understood that it have to be.
22nd Aug 2018, 1:28 PM
Ilyich
+ 1
Ilyich Oh alright, well, good luck my friend : )
22nd Aug 2018, 1:41 PM
Ipang