一个特定的变量没有定义我不知道为什么

One specific variable is not define Idon't Know why

我正在开展一个 python 项目,简而言之,这是一项基于 pplapi 数据库的社会研究。我的第一项研究是关于 link 一个人的年龄和她的 wealth.From 一本字典,我挑选了给我年龄的信息和给我他们的财富的信息,然后我将这些信息放入矩阵中,然后绘制所有内容。我的第二项研究是关于 link 幸福感与某人离开的城市规模之间的关系。和以前一样,我在同一本字典中获取我需要的信息,我将所有内容放入矩阵中并绘制所有内容。我正在尝试进行一些对象编程,但我是新手。

问题出在我的 class 区域,我定义了一个名为“H”的变量,但是 python 说这个变量没有定义。我想我放在函数上方的“@class方法”有一个技巧,但我不知道该怎么做才能修复所有问题。有人可以帮助我吗?如果可能的话,请解释一下当我执行此命令时会发生什么?我添加了 Python 给我的错误消息的图像。

import json 
import math
class Agent: # Les class n'ont pas de () à la fin 

    def dire_bonjour(self,prenom):
        return "Bonjour {} !".format(prenom)
    def __init__(self,position,**agent_attributes):
        self.position = position 
        for attr_name, attr_value in agent_attributes.items():
            setattr(self,attr_name,attr_value)
class Position:
    def __init__(self, abscisses_degrees, ordonnees_degrees):
        self.abscisses_degrees = abscisses_degrees
        self.ordonnees_degrees = ordonnees_degrees
    @property
    def abscisses_rad(self):
        return self.abscisses_degrees * math.pi / 180 
    @property 
    def ordonnees_rad(self):
        return self.ordonnees_degrees * math.pi / 180
class Zone:
    ZONES = []
    MIN_LONGITUDE_DEGREE = -180
    MAX_LONGITUDE_DEGREE = 180
    MIN_LATITUDE_DEGREE = -90
    MAX_LATITUDE_DEGREE = 90
    DDEGREES = 1
    Η = 1

    def __init__(self, corner1,corner2):
        self.corner1  = corner1
        self.corner2 = corner2 
        self.inhabitants = 0
    @classmethod
    def initialize_zones(cls):
        for abscisses in range(cls.MIN_LATITUDE_DEGREE,cls.MAX_LATITUDE_DEGREE,H):
            for ordonnees in range(cls.MIN_LONGITUDE_DEGREE,cls.MAX_LONGITUDE_DEGREE,DDEGREES):
                bottom_left_corner = Position(longitude,latitude)
                top_right_corner = Position(longitude+cls.DDEGREES,latitude+H)
                zone = Zone(bottom_left_corner,top_left_corner) 
                cls.ZONES.append(zone)
        print(len(cls.ZONES))

def main():
    for agent_attributes in json.load(open("agents-100k.json")):
        abscisses = agent_attributes.pop("latitude")  #Latii est couchée....(latitude)
        ordonnees = agent_attributes.pop("longitude") # pour ne prélever que la valeur souhaitée, utiliser agent_attributes.pop(str)
        position = Position(abscisses,ordonnees)
        agent = Agent(position,**agent_attributes)
        Zone.initialize_zones()

为了方便起见,我在此处放置了一个 git 中心 link,您可以在其中找到我尝试重现的代码,您还可以找到我正在使用的数据库.
https://github.com/OpenClassrooms-Student-Center/la_poo_avec_python/tree/04_class_methods

您在 class 中定义了 H,需要将其称为 self.H。您还有 class 方法需要声明为接受 self 作为第一个参数。

由于您将其装饰为 class 方法 (@classmethod),因此您需要将其作为 class 变量进行访问 cls.H.

也许下面的小例子会对你有所帮助

class base_foo:

    cls_variable = "I'm in Class...."
    @staticmethod
    def say_static_hello():
        print("Hello...")

    @classmethod
    def say_class_hello(cls):
        if(cls.__name__=="class_foo"):
            print(cls.cls_variable)
            print("Hello foo")
        elif(cls.__name__=="class_bar"):
            print(cls.cls_variable)
            print("Hello bar")

class class_foo(base_foo):
    def say_class_hello(self):
        print("Class foo")

class class_bar(base_foo):
    def say_static_hello(self):
        print("Class bar")


test_foo = class_foo()
test_foo.say_class_hello()
test_foo.say_static_hello() 

test_bar = class_bar()
test_bar.say_class_hello()
test_bar.say_static_hello()

输出:

Class foo
Hello...
I'm in Class....
Hello bar
Class bar

编辑:

有问题,所以我修改了代码,会介意用下面的文件执行。单独的文件?

为什么?

您的文件中有一个 Diacritics(非 Ascii):) 这就是原因。看看这个 https://pteo.paranoiaworks.mobi/diacriticsremover/

import math
class Agent: # Les class n'ont pas de () à la fin 

    def dire_bonjour(self,prenom):
         return "Bonjour {} !".format(prenom)
    def __init__(self,position,**agent_attributes):
         self.position = position 
         for attr_name, attr_value in agent_attributes.items():
              setattr(self,attr_name,attr_value)
class Position:
    def __init__(self, abscisses_degrees, ordonnees_degrees):
         self.abscisses_degrees = abscisses_degrees
         self.ordonnees_degrees = ordonnees_degrees
    @property
    def abscisses_rad(self):
         return self.abscisses_degrees * math.pi / 180 
    @property 
    def ordonnees_rad(self):
         return self.ordonnees_degrees * math.pi / 180
class Zone:
    ZONES = []
    MIN_LONGITUDE_DEGREE = -180
    MAX_LONGITUDE_DEGREE = 180
    MIN_LATITUDE_DEGREE = -90
    MAX_LATITUDE_DEGREE = 90
    DDEGREES = 1
    H = 1

    def __init__(self, corner1,corner2):
         self.corner1  = corner1
         self.corner2 = corner2 
         self.inhabitants = 0
    @classmethod
    def initialize_zones(cls):
         for abscisses in range(cls.MIN_LATITUDE_DEGREE,cls.MAX_LATITUDE_DEGREE,cls.H):
              for ordonnees in range(cls.MIN_LONGITUDE_DEGREE,cls.MAX_LONGITUDE_DEGREE,DDEGREES):
                    bottom_left_corner = Position(longitude,latitude)
                    top_right_corner = Position(longitude+cls.DDEGREES,latitude+cls.H)
                    zone = Zone(bottom_left_corner,top_left_corner) 
                    cls.ZONES.append(zone)
         print(len(cls.ZONES))

def main():
    for agent_attributes in json.load(open("agents-100k.json")):
         abscisses = agent_attributes.pop("latitude")  #Latii est couchée....(latitude)
         ordonnees = agent_attributes.pop("longitude") # pour ne prélever que la valeur souhaitée, utiliser agent_attributes.pop(str)
         position = Position(abscisses,ordonnees)
         agent = Agent(position,**agent_attributes)
         Zone.initialize_zones()