Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Missing one positional required argument `self`

I am trying to develop a function using OOPs. But I am getting following error. The function basically for the Raspberry Pi. It has been working fine when I use it without any OOPs structure.

Code:

import Adafruit_ADS1x15 as ada_adc

class read_raspberrypi_analog_input:
    ## Default level
    
   
    # The following is executed when self is called
    def __init__():
        # call an instance of ADS1015 if it is used for readin analog input signal
        self.adc = ada_adc
        

    def through_ads1015(self, adc_gain = 1, r1 = 1, r2 = 0):
        adc = self.adc.ADS1015()

        GAIN = adc_gain

        # read the value at the board input analog pin A0
        a0_level = adc.read_adc(0, gain=GAIN)
        print(a0_level)
        a0_analog = a0_level*(4.096/2047)
        print(a0_analog)
        # actual sensor input voltage
        r1 = r1
        r2 = r2
        a0_sensor = a0_analog *(r1+r2)/r1
        print(a0_sensor)

Call the class and method:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

read_raspberrypi_analog_input.through_ads1015(adc_gain = 1, r1 = 5.1, r2 = 3)

Present output:

    read_raspberrypi_analog_input.through_ads1015(adc_gain = 1, r1 = 5.1, r2 = 3)
TypeError: through_ads1015() missing 1 required positional argument: 'self'

>Solution :

You’re not instantiating your class at all, you’re trying to call a method on the class itself, and if you’re not passing in self explicitly, well, you’re missing that positional argument.

Chances are you’ll also want to initialize that ADC object only once when you construct your wrapper object, like so – in your original code, you were just storing a reference to the module.

import Adafruit_ADS1x15


class ReadRaspberryPiAnalogInput:
    def __init__(self):
        self.adc = Adafruit_ADS1x15.ADS1015()

    def through_ads1015(self, adc_gain=1, r1=1, r2=0):
        # read the value at the board input analog pin A0
        a0_level = self.adc.read_adc(0, gain=adc_gain)
        print(a0_level)
        a0_analog = a0_level * (4.096 / 2047)
        print(a0_analog)
        # actual sensor input voltage
        a0_sensor = a0_analog * (r1 + r2) / r1
        print(a0_sensor)

ai = ReadRaspberryPiAnalogInput()
print(ai.through_ads1015(adc_gain = 1, r1 = 5.1, r2 = 3))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading