Questions about code from SoloLearn lessons | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 1

Questions about code from SoloLearn lessons

Questions: 1)Why in lines with "GameObject.objects ..." aren't any spaces, tabs?-Because it's long line? 2)What do lines with "GameObject.objects ..." mean? I don't understand these parts of code. 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) Thank in advance for answers!

18th Aug 2018, 7:07 PM
Ilyich
4 Answers
+ 3
ah ok - in most languages coders like to make these 'indentations' because it helps with the readability of the code. It helps you see what statements are in the class, or if, or while etc. Python goes a step further, these are actually *required* for the program to function correctly (it's a nice idea, since everyone does it anyway and is part of the recommended style for most languages) At the top of the class GameObject we have defined its properties, one of them is called objects which is a dictionary. In order to refer to this property of GameObject, we use the dot. In __init__ we give it a key value pair, in examine() I believe we are looping through its keys to check for the existence of the required noun
19th Aug 2018, 7:03 AM
Dan Walker
Dan Walker - avatar
+ 1
-) I don't mean spaces in this line. I mean tabs before this line of code. If we use clases or functions or while and ect. , code in these pices of code will be with tabs before. -)If someone can, please just say me what the lines which were minded above do. I know many methods, function but I don't know how work ".object" and what do lines above
18th Aug 2018, 9:07 PM
Ilyich
+ 1
I found this module to be really confusing and much unlike all previous ones where everything had been explained very well and in detail. Perhaps it was only to demonstrate what can be done, but understanding the full code was a bit of a challenge. def __init__(self, name): self.name = name GameObject.objects[self.class_name] = self For me, the line beginning GameObject is indented and inside the function, so I don't know why yours isn't. To answer your second question, though, the line... GameObject.objects[self.class_name] = self ...does the following. To understand what it does, it's best to look at the goblin subclass. class Goblin(GameObject): def __init__(self, name): self.class_name = "goblin" self.health = 3 self._desc = " A foul creature" super().__init__(name) Here, the super() method is calling its superclass's __init__ method, which has one attribute, name. So this runs the code: def __init__(self, name): self.name = name GameObject.objects[self.class_name] = self Now you can see that this creates an entry in the objects dictionary. The key is the class name (e.g. goblin), and the value is the subclass (I think). if you do: print(GameObject.objects) This shows you that the values of the dict are in fact class objects (e.g. .Goblin). Now as for why this line is there, it is required to run the examine() and hit() methods, and any other verbs which reference the name of a gameobject. That's what I got from it at least.
23rd Feb 2019, 4:37 PM
Callum Evans
Callum Evans - avatar
0
1) That's just generally how code is written. Spaces separate stuff, and so GameObject must be one word. You can split at the dots, but most IDEs/editors will strip extra internal spaces out, or you can modify the settings to have them (typically in method parameter lists). You may find several dots being split across lines such as GameObject.objects() .getProperty() .getParameter() (this is meaningless, just an example) 2) An object can have properties and methods, and the dot is the way we access them. So a Person can have a name, an age, and can also do stuff like cook(), work() etc. so you can do person.name, person.age to access those properties. For other libraries there will be documentation saying what those properties are so you can use them in your code
18th Aug 2018, 8:26 PM
Dan Walker
Dan Walker - avatar