0

I am new to the OOP with Python. Until now I have worked with OOP languages as C++, C#, and Java. Since I have to translate a C# module to Python, I encountered conceptional differences.

I have this C# class relationship:

 internal class ChirpMeasDataClass
  {
     public ChirpMeasDataClass(List<AntennaLevelDataClass> AntennaLevelData, List<DiffphaseDataClass> DiffphaseData)
     {
        this.AntennaLevelData = AntennaLevelData;
        this.DiffphaseData = DiffphaseData;
     }
     internal List<AntennaLevelDataClass> AntennaLevelData = new List<AntennaLevelDataClass>();
     internal List<DiffphaseDataClass> DiffphaseData = new List<DiffphaseDataClass>();
  }
  internal class AntennaLevelDataClass
  {
     internal double dLevel =  -10000;          // [dB]
     internal double dVarianzLevel = -10000;  // [dB^2]
  }
  internal class DiffphaseDataClass
  {
     internal double dDiffPhase = -10000;         // [°] 
     internal double dVarianzDiffPhase = -10000; // [°^2] 
  }

What intuitively commes to mind in python is to have something like this:

class AntennaLevelDataClass:
    def __init__(self):
        self.dLevel = -10000  # [dB]
        self.dVarianzLevel = -10000  # [dB ^ 2]

class DiffPhaseDataClass:
    def __init__(self):
        self.dDiffPhase = -10000 # [°]
        self.dVarianzDiffPhase = -10000 # [° ^ 2]

class ChirpMeasDataClass:
    def __init__(self,AntennaLevelDataClass,DiffPhaseDataClass):
       self.antennaLevelDataList=AntennaLevelDataClass
       self.diffPhaseDataList=DiffPhaseDataClass

or something like this :

   class ChirpMeasDataClass:
        def __init__(self,antennaLevelDataClass,diffPhaseDataClass):
           self.antennaLevelDataList=[]
           self.diffPhaseDataList=[]

What is the common way to have list of Objects as an attribute in a class (instance attribute in Python)?

1
  • 1
    Python isn't typed (footnote, asterisk, it has optional type annotations now…), so the only thing you do is take an argument in the constructor and assign it to a property. That's it. That argument is supposed to be a list containing only instances of a specific type, but all that is rather irrelevant and doesn't need to be declared in any specific way. So, def __init__(self, a, b): self.a = a; self.b = b is really all you need. Commented Sep 4, 2018 at 9:38

2 Answers 2

1

The only thing that C# constructor does is assign two arguments, which are lists of objects, to two instance properties. The rest is largely type annotations, which are entirely optional in Python. So, the basic thing you want is just:

class ChirpMeasData:
    def __init__(self, antenna_level_data, diff_phase_data):
       self.antenna_level_data = antenna_level_data
       self.diff_phase_data = diff_phase_data

You could add type annotations:

from typing import List

class ChirpMeasData:
    def __init__(self, antenna_level_data: List[AntennaLevelData],
                 diff_phase_data: List[DiffphaseData]):
       self.antenna_level_data = antenna_level_data
       self.diff_phase_data = diff_phase_data

(Note that I'm foregoing the redundant use of "Class" in the class names here.)

Sign up to request clarification or add additional context in comments.

Comments

0

If you do

self.antennaLevelDataList=AntennaLevelDataClass

Then self.antennaLevelDataList is an instance variable, and its value is that class definition. That doesn't seem very useful.

Moreover, you're passing two class definitions into the constructor.

Using lists (of which the contents happen to be instance of those classes) is much cleaner, and C++ is also using Lists so it's mostly a direct equivalent. The only difference is that in C++ you have to declare what type the contents of the list are and in Python you can't.

In the constructor, it's probably useful to copy the contents of the lists that are passed in, something like:

class ChirpMeasDataClass:
    def __init__(self, antennaLevelData, diffPhaseData):
        self.antennaLevelDataList = anntennaLevelData[:]
        self.diffPhaseDataList = diffPhaseData[:]

([:] indicates a slice of the entire list, in effect a shallow copy)

The point is that otherwise the instance attributes refer to the same list object as the ones passed in, and that might lead to surprises if one is copied; nothing is every copied implicitly in Python.

3 Comments

"and its value is that class definition" – OP is shadowing the name AntennaLevelDataClass with a parameter there…
"and in Python you can't" – …and in Python it's optional [and not enforced by the runtime].
"nothing is every copied implicitly in Python" – Right, which is why implicitly making a copy of the list in the constructor is arguably the surprising behaviour.

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.