First we import the random library (you may need to install it), from which we need the function choice () that allows you to select a random object from the list. Import with keyword import and the names of libraries are usually always held at the beginning of the file.
Create a list with greetings. The list is one of the main ways of storing data in Python, the analogue of an array in other languages. How they work and what they can do, we will examine a little later, but just notice: the lists allow you to store a set of different elements in a clearly defined sequence.
Create tuples with possible moods and grades of employees. The main difference of a tuple from the list is that its contents cannot be changed. Since we assume that the set of attitudes and grades will not be changed, we record the variables that store them in large letters.
Create a class of civil servants using the class keyword. Note the syntax: the class name is written with a capital letter and brackets are used optional, if you inherit properties and methods from another class — but more about that later.
The content of the class is a block of code, and therefore after the colon is indented four spaces. In the triple quotation marks is annotated with a description of the class or function. This is a standardized comment in Python, allowing to understand the person reading the code why we need this class or this function.
The contents of the special method __init__() is executed whenever we create an object based on the class. Each sample is assigned the class specified in __init__() features, in our case, the rank and the mood that each civil servant will be random. In order to extract tuples rank and mood, we use a function choice() from library random. Access any function from third party library can be obtained by writing the library name and function name separated by a dot.
Perhaps your attention was drawn to the self-keyword which is used many times within the class. This is one of the most unintuitive things in the early stages of learning programming. The point is that in Python, the entry rank = random.choice(RANKS) will be incorrect within the class. The word self helps the interpreter to understand that we assign some value to one of the infinite number of possible samples of the class. I am not going to go into detail, but just draw your attention — when you are planning what will be the sample class, you definitely need to use the word self — it includes a special method __init__ () where it should be placed in brackets.
Function greet() of class Bureaucrat gives a random greeting, and makes it clear what the properties of the created employee. After the class description, we give only two commands: assign a variable the bureaucrat sample class and use the method greet () to understand what the employee was created and assigned to the variable bureaucrat. Since we use the random function.choice() inside the class, every sample-class Bureaucrat will be with high probability different from the previous ones.
That’s all for now. Today we learned the basics of object-oriented programming by creating a simple class public servant, and laid part of the Foundation of the game. Next time we will continue to explore the possibilities of classes, carefully consider them, and the structure of the game will start to emerge more clearly.
I understand that perhaps not all the explanations were accurate, because we go to quite a complex area. If you do not understand something, write comments under the post and in social media — I will be glad and any other feedback. If you are more experienced in programming people than I, with pleasure will listen to substantive criticism. Thanks, and until next time!