1

I have some code like:

class Digital_signal_information:
    def __init__(self, signal_power :float, noise_power :float, n_bit_mod:int):
        self.signal_power=signal_power # The attribute i want to use
        self.noise_power=noise_power
        self.n_bit_mod=n_bit_mod


class Line(Digital_signal_information):
    def __init__(self,loss_coefficient:float, length:int):
        self.loss_coefficient=loss_coefficient
        self.length=length

    def Noise_Generation(self): #Here i need to use it
        noise_generation=1e-9*self.signal_power*self.length
        return noise_generation
    def SNR_Digital(self): # Also here
        snr_digital=self.signal_power-self.noise_power-self.loss
        return snr_digital

How can I use self.signal_power in the indicated Line methods?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Severjan Lici
  • 371
  • 1
  • 10
  • @rv.kvetch Just that? No aditional stuff to write? – Severjan Lici May 02 '23 at 23:33
  • 1
    "call an attribute" doesn't make any sense at all, and data doesn't go "from" or "into" classes. It **seems like** what you mean is "I need to use an attribute defined in the base class from a method defined in the derived class", but it's hard to tell. – Karl Knechtel May 02 '23 at 23:35
  • Anyway, yes; the reason that `self.signal_power` doesn't work in those methods is because instances of `Line` don't actually have that attribute, which is because the superclass (base class)'s `__init__` wasn't called. This is what `super` is for. – Karl Knechtel May 02 '23 at 23:36
  • As an aside: please keep in mind that this is **not a discussion forum**. There is no standard for "difficulty" of questions, but there is a very high standard (relative to other places on the Internet, at least) for how the questions are asked. In particular, questions should be about **the question, not** you, your level of experience, or how you feel about asking the question. I edited the question to show proper style, and also to remove irrelevant details from the code to show what a proper [mre] looks like. – Karl Knechtel May 02 '23 at 23:39
  • @KarlKnechtel My question **WAS** first of all about **a question**, my experince was an excuse as to why im asking such a basic question. I apologize if for some reason my inexperience hurt you but we all start somewhere and since i cant ask anyone directly for help i came here. – Severjan Lici May 02 '23 at 23:41
  • Please read the edited question to understand the issue. – Karl Knechtel May 03 '23 at 00:19
  • I fixed the duplicate link with something more specific to the problem. – Karl Knechtel May 05 '23 at 03:00

1 Answers1

0

Currently, you have the following class definitions:

class DigitalSignalInformation:
    def __init__(self, signal_power: float, noise_power: float, n_bit_mod: int):
        self.signal_power = signal_power  # The attribute i want to call
        self.noise_power = noise_power
        self.n_bit_mod = n_bit_mod


class Line(DigitalSignalInformation):
    def __init__(self, loss_coefficient: float, length: int):
        self.loss_coefficient = loss_coefficient
        self.length = length

If you are using PyCharm, it will actually give you a warning that super call is missed:

enter image description here

With the super().__init__() call:

class DigitalSignalInformation:
    def __init__(self, signal_power: float, noise_power: float, n_bit_mod: int):
        self.signal_power = signal_power  # The attribute i want to call
        self.noise_power = noise_power
        self.n_bit_mod = n_bit_mod


class Line(DigitalSignalInformation):
    def __init__(self, loss_coefficient: float, length: int,
                 # set defaults here -- used for DigitalSignalInformation.__init__
                 signal_power: float = 1.23,
                 noise_power: float = 4.56,
                 n_bit_mod: int = 7):
        
        # call DigitalSignalInformation.__init__()
        super().__init__(signal_power, noise_power, n_bit_mod)

        self.loss_coefficient = loss_coefficient
        self.length = length
rv.kvetch
  • 9,940
  • 3
  • 24
  • 53
  • In the ```class Line``` i must not define the ```class DigitalSignalInformation```. I must do that outside in another function that wil be called ```exercise```. In the ```class Line``` i only need to use the ```signal_power``` from the ```class DigitalSignalInformation```. Any way you can modify the code? – Severjan Lici May 02 '23 at 23:43
  • 1
    your class `Line` subclasses from `DigitalSignalInformation`, so in a sense you already "define" it. There is no way getting aroudn that. If you mean is there a way to avoid calling constructor of super class `DigitalSignalInformation` from `Line`, the answer is **no**. You cannot miss super class call in `__init__()` of any subclass, in general. – rv.kvetch May 02 '23 at 23:52
  • What if i use a getter and setter in place of a constructor? – Severjan Lici May 02 '23 at 23:54
  • @SeverjanLici yes, that works. you would just need to move those attributes from `DigitalSignalInformation` into `Line`. But note, that you would still need to set values for those attributes in `Line` (unless you have default values already) – rv.kvetch May 03 '23 at 00:21