Referencing class instance through lists

Holsterbau

I have two classes. Province creates a bunch of instances of Areas, and puts them in its self.areas list. I want for an Areas instance to access the attributes (or other data) of the Province instance which contains it in its self.areas list. To visualize:

class Province:
    def __init__(self, stuff="spam"):
        self.stuff = stuff
        self.areas = list()
    def makeareas(self):
        # make instances of Areas and put them in self.areas

class Areas:
    def __init__(self):
        pass
    def access_stuff(self):
        # access stuff of the Province where it is in its list

How do I accomplish this? More importantly, is this even a correct approach? Is there a more reasonable and easier way to do this that I don't know of?

Stephen Rauch

When instantiating an Area pass it a reference to province like:

Code:

class Province:
    def __init__(self, stuff="spam"):
        self.stuff = stuff
        self.areas = list()

    def makeareas(self, area):
        self.areas.append(Areas(area, self))

class Areas:
    def __init__(self, area, province):
        self.area = area
        self.province = province

    def access_stuff(self):
        # access stuff of the Province where it is in its list
        return '%s - %s ' %(self.area, self.province.stuff)

Test Code:

p = Province('This is stuff')
# Examples
p.makeareas('area1')
p.makeareas('area2')

for area in p.areas:
    print(area.access_stuff())

Results:

area1 - This is stuff 
area2 - This is stuff 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Referencing instance variables from within abstract class

Referencing a type or class through an enum value?

Iterating through an instance of a class in Python

Referencing an instance type of a child class in base class constructor parameter in typescript

Referencing an instance of a class created in main from another class module

Referencing instance method 'findIdxOf' on 'Array' requires that 'FeedData' be a class type

One instance attribute referencing another, with updates after class instantiation

How does referencing a class instance inside constructor work?

Creating an instance of class using lists and dictionaries

Class Referencing

Getting type class instance through a parent type

See if an object is an instance of a class passed through a string

Calling a class method through an object instance

Coffeescript class instance in callback through classes

Initialize a new instance of the class through its method

Looping through a class instance's enum

Iterating through instances of a class by instance attribute

pygame looping through a movement with a Class instance

Excel Cross referencing lists

JavaScript: Create and destroy class instance through class method

Changing the values of an instance of a class through another class in python

How to find a specific instance of a class through a class variable?

Get an instance of static class with a private constructor through generic class

how to compare two Lists of a class and return an instance of that class when appearing in both lists

C# Add List/Lists to Data Class instance

Iterating through list of lists of class objects, returns that object is not iterable?

Referencing an instanced class inside another instance but not allowing change of such value after its been initialized

Referencing a list from list of lists

Persist C++ class instance through Matlab mex function calls