一个特定的变量不是定义,我不知道为什么

2024-06-25 23:39:40 发布

您现在位置:Python中文网/ 问答频道 /正文

enter image description here

我正在做一个python项目,这是一个基于pplapi数据库的社会研究。我的第一项研究是关于一个人的年龄和她的财富之间的联系。从字典中,我提取了提供人们年龄的信息和提供他们财富的信息,然后将这些信息放入矩阵,然后绘制出所有信息。我的第二项研究是关于幸福感与人们离开的城市大小之间的联系。和以前一样,我在同一本字典里找到了我需要的信息,我把所有的东西都放进一个矩阵,然后画出所有的东西。我正在尝试做一些对象编程,我是新手

问题是在我的类区域,我定义了一个名为“H”的变量,但是python说这个变量没有定义。我想我在函数上方加了一个“@classmethod”的小把戏,但我不知道该怎么做才能解决所有问题。有人能帮我吗?如果可能的话,解释一下当我执行这个命令时会发生什么?我添加了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 hub链接,您可以在这里找到我试图复制的代码,也可以找到我正在使用的数据库。
https://github.com/OpenClassrooms-Student-Center/la_poo_avec_python/tree/04_class_methods


Tags: inself信息fordefpositionattributesclass
2条回答

由于您将其修饰为类方法(@classmethod),因此需要将其作为类变量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()

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

相关问题 更多 >