简体   繁体   English

我想在另一个 class 中使用来自 class 的列表,但我无法实现上述目标

[英]I want to use a list from a class in another class, but i am not being able to achieve said goal

So, i am doing a project and i need to refer, in a class, a list belonging to another class, heres what i got so far:所以,我正在做一个项目,我需要在 class 中引用属于另一个 class 的列表,这是我到目前为止得到的:

import pandas as pd
import numpy as np

data = pd.read_excel(r'D:\Coisas fixes\London.connections.xlsx')
df = pd.DataFrame(data, columns=['station1', 'station2'])

data2 = pd.read_excel(r'D:\Coisas fixes\London.stations.xlsx')
df2 = pd.DataFrame(data2, columns=['id', 'name'])


class testing_tests:

    def __init__(self):
        self.edge = []
        self.vector = []
        np_array = df.to_numpy()
        for i in np_array:
            no1, no2 = i
            self.edge.append((no1, no2))
        print(self.edge)


class Edge:

    def __init__(self):
        for i in range(len(testing_tests.edge)):
            self.v1 = df.iloc[i, 0]
            self.v2 = df.iloc[i, 1]

Basically whats happening is me creating a program to read an excel file, which i managed to do, and save some values in a new class, but i am not figuring out a way of acessing the list in the other class, heres what i wrote on the python console.基本上发生的事情是我创建了一个程序来读取 excel 文件,我设法做到了,并将一些值保存在新的 class 中,但我没有想出一种方法来访问其他 ZA2F2ED4F8EBC2CBBD4C21A2 中的列表在 python 控制台上。

c = testing_tests()
-[(11, 163), (11, 212), (49.........
E = Edge()
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:/Users/vasco/Downloads/Ze wrk.py", line 26, in __init__
    for i in range(len(testing_tests.edge)):
AttributeError: type object 'testing_tests' has no attribute 'edge'

I put the multiple dots because the result is like 400 numbers.我放了多个点,因为结果就像 400 个数字。 Anyone knows what to do?有谁知道该怎么做? All help is apreciated感谢所有帮助

c = testing_tests()

Here you are creating an instance of the testing_tests class, and assigning that instance to c .在这里,您正在创建testing_tests class 的实例,并将该实例分配给c The __init__ will run o initialise the class instance, setting up the self.edge ( c.edge ) list. __init__将运行初始化 class 实例,设置self.edge ( c.edge ) 列表。

But inside your Edge class, you have但是在你的Edge class 里面,你有

for i in range(len(testing_tests.edge)):

Here, you're not using your instance, you're referring directly to the testing_tests class (not an instance of it).在这里,您没有使用您的实例,而是直接引用testing_tests class (不是它的实例)。 The class (which is like a template) doesn't have an edge attribute. class(类似于模板)没有edge属性。 Only instances of the class have that.只有 class 的实例有这个。

One solution is to make an instance inside Edge, eg一种解决方案是在 Edge 中创建一个实例,例如

class Edge:
    def __init__(self):
        self.tt = testing_tests()
        for i in range(len(self.tt.edge)):

As an aside, this sort of confusion between classes and their instances is a good example of why using a naming convention can make code more readable.顺便说一句,类和它们的实例之间的这种混淆是一个很好的例子,说明为什么使用命名约定可以使代码更具可读性。 If you use CamelCase for your class names, it's easier to spot this sort of issue.如果您将CamelCase用于您的 class 名称,则更容易发现此类问题。 Eg例如

class TestingTests:
    def __init__(self):
        self.edges = []
        np_array = df.to_numpy()
        for i in np_array:
            no1, no2 = i
            self.edges.append((no1, no2))
        print(self.edges)

class Edge:
    def __init__(self):
        self.tt = TestingTests()
        for i in range(len(self.tt.edges)):
            self.v1 = df.iloc[i, 0]
            self.v2 = df.iloc[i, 1]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我想从数据集创建列表并在 palantir 铸造厂的另一个 function 中使用,但找不到任何解决方案 - I want to create the list from the datasest and use in another function in palantir foundry but not able to find any solution 我希望能够从根路径和子路径加载类 - I want to be able to load class from both root and sub path 为什么我不能从这个全局变量中调用 class? - Why am I not able to call a class from this global variable? 如果我不想使用类,如何从另一个函数访问动态变量? - How to access dynamic variable from another function if I don't want use class? 我无法在python中将代码从一个文件导入到另一个文件中。 - i am not being able to import code from one file to another in python.I am working on sublime text 如何在 Python 中的另一个类中使用一个类中的变量? - How do I use a variable from a class in another class in Python? 我无法从列表中删除元素 - I am not able to remove elements from the list 我正在尝试学习如何从另一个文件(我制作的)导入和使用 class。 为什么我的 output 不显示 7? - I am trying to learn how to import and use a class from another file (that I made). Why isn't my output showing 7? 为什么我可以访问列表类型的 class 变量并更改其内容,但不能访问字符串类型的变量? - Why am I able to access the class variable of type list and change its contents but not of type string? 为什么我不能从由另一个列表中的元素过滤的列表中选择某些元素? - Why am I not able to select some elements from a list filtered by elements in another list?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM