The initial variable of the Python class.

This is my first question, so sorry ... I'm starting with python and coding in general, and I wanted to create a class called "Map" that would have the following class variables:

class Map: height = 11 width = 21 top = [['#']*width] middle = [['#']+[' ']*(width-2)+['#'] for i in range(height-2)] field = top + middle + top b = Map() Shell: >>> middle = [['#']+[' ']*(width-2)+['#'] for i in range(height-2)] NameError: name 'width' is not defined 

If I put the variables outside the class, it will work. What am I doing wrong?

Thanks for the help.

+7
python class class-variables
source share
2 answers

From docs :

Names refer to objects. Names are entered using name binding operations. Each occurrence of a name in the program text refers to the binding of this name installed in the innermost function block containing the use.

A block is part of the text of a Python program that runs as a unit. The following are the blocks: module, function body, and class definition. Each command entered interactively is a block. A script file (a file provided as standard input to the interpreter or specified as a command line argument to the interpreter) is a block of code. The script command (the command specified on the interpreter command line with the "-c" parameter) is a block of code. The string argument is passed to the built-in functions eval (), and exec () is the code block.

The code block is executed in the execution frame. The frame contains some administrative information (used for debugging) and determines where and how execution continues after the completion of code blocks.

The scope determines the visibility of the name inside the block. If a local variable is defined in a block, its scope includes this block. If the definition occurs in a function block, the scope extends to any blocks contained in the defining block, unless the contained block introduces another binding for the name. The scope of names defined in a class block is limited to a class block; it does not apply to code blocks of methods - this includes expressions of concepts and generators, since they are implemented using the scope of functions . This means that the following will fail:

 class A: a = 42 b = list(a + i for i in range(10)) 

The comps list in python3 has its own scope, unlike python2, where your code will work as it is.

If you take the following example using python2, you can see how variables flowing in the comp comp list area can cause some problems:

 class A: a = 42 b = [a for a in range(10)] a = A() print(aa) 9 

You have several options; you can use __init__ create instance attributes:

 class Map: def __init__(self): self.height = 11 self.width = 21 self.top = [['#']*self.width] self.middle = [['#']+[' ']*(self.width-2)+['#'] for i in range(self.height-2)] self.field = self.top + self.middle + self.top m=Map() print(m.field) 

Using Method:

 class Map: @staticmethod def meth(): height = 11 width = 21 top = [['#']*width] middle = [['#']+[' ']*(width-2)+['#'] for i in range(height-2)] field = top + middle + top return field b = Map() print(b.meth()) 

What you choose really depends on what you want to do.

+7
source share

you need to use b.width and b.height to specify member variables. Refer to this post for some explanation on accessing member variables - Accessing member variables of a class in Python?

0
source share

All Articles