0

I would like to store the instance of a class in a container like a list. Other classes/methods should access this instance.

Below is a code snipped which defines a data point.

class dataPoint(object):
    def __init__(self, name, typeUUID, value = None):
        self.name = name
        self.typeUUID = typeUUID
        self.value = value

I like to define a method which gives me the reference (no copy constructor, etc.) to this object. Maybe like this:

def getRef(self):
    return ???

These references I like to use in different list. The reference I like to use to set properties/call functions of the data point. Below is some pseudocode:

# define a conatiner with datapoints    
myContainer = [dataPoint("temperature","double",273.15), dataPoint("power","double",230), dataPoint("errorcode","uint32",666)]

# define interfaces which refers to the datapoints
interface1 = [ref("temperature"), ref("power")]

interface2 = [ref("errorcode"), ]

interface3 = [ref("temperature"), ref("power"), ref("errorcode")]
# set temperature to 300K
ref("temperature") = 300.0

# interfaces
print (interface1[ref("temperature")]) --> 300K
print (interface3[ref("temperature")]) --> 300K

How to do this in Python and how to do this pythonic?

3
  • 1
    myContainer is the only thing that knows about the specific instances of dataPoint; neither any specific instance of dataPoint or the class itself tracks information about other instances. Commented Sep 29, 2017 at 13:50
  • 1
    Put another way, getRef would be a method of another class that can track groups of related dataPoint instances. (As Tom Dalton shows in his answer, DataPoint can be instrumented to act as the instance tracker.) Commented Sep 29, 2017 at 13:51
  • Is this the common way to solve that kind of problems or is this only a particular solution for my problem? Commented Sep 29, 2017 at 14:13

1 Answer 1

1

You could put the "instance-container" in the class itself:

class DataPoint:
    instances = {}

    def __init__(self, name, typeUUID, value=None):
        self.name = name
        self.typeUUID = typeUUID
        self.value = value

        self.instances[name] = self

    @classmethod
    def get(cls, name):
        return cls.instances[name]

Then you can use it like this:

>>> d1 = DataPoint("foo", "12345")
>>> d2 = DataPoint("bar", "67890")

>>> DataPoint.get("foo")
<DataPoint object at 0x.........>
Sign up to request clarification or add additional context in comments.

2 Comments

In you approach 'DataPoint' holds all instances of it and managing them. Looks like a solution. What is with a more C/C++ concept of passing (smart) pointers through the system?
All python objects are 'smart pointers' e.g. all variables are references.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.