简体   繁体   中英

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:

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.

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. 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 . The __init__ will run o initialise the class instance, setting up the self.edge ( c.edge ) list.

But inside your Edge class, you have

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). The class (which is like a template) doesn't have an edge attribute. Only instances of the class have that.

One solution is to make an instance inside Edge, eg

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. 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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM