1

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:

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'
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Mainland
  • 4,110
  • 3
  • 25
  • 56
  • 1
    Oops, you forgot to create an instance of the class. – mkrieger1 Jul 17 '23 at 16:39
  • @mkrieger1 spot on I forgot `OOPs` instance. – Mainland Jul 17 '23 at 16:41
  • I'm not sure you need to hype up the "OOP" part of this. It's not 1980. OOP is taken for granted in languages like Python, it's not exotic. If you want to adapt procedural-style code into a more class-based structure, that's certainly a better way to organize things. – tadman Jul 17 '23 at 16:42
  • Does this answer your question? [TypeError: Missing 1 required positional argument: 'self'](https://stackoverflow.com/questions/17534345/typeerror-missing-1-required-positional-argument-self) – mkrieger1 Jul 17 '23 at 16:51
  • 1
    @tadman there is an unfortunate way that the term is used that basically means "writing a class definition". – juanpa.arrivillaga Jul 17 '23 at 16:52
  • @juanpa.arrivillaga I get that, but it's also a somewhat dated term as OOP has fragmented into many more concerns, like design patterns, encapsulation, etc. In the Smalltalk era this was a big deal, though, as most languages had no ability to do it. Now Python, Ruby, Node, C#, Java, Swift, you name it, they all have objects. It's just another tool you can use, not something you need to draw attention to specifically. – tadman Jul 17 '23 at 16:54
  • @juanpa.arrivillaga You are spot on. All these years I have been using simple functions. Recently I learned about the advantages of OOPS ("writing a class definition") in Python and I started. – Mainland Jul 18 '23 at 04:18
  • @tadman I have no idea that now all languages offer this wonderful feature. – Mainland Jul 18 '23 at 04:19
  • Exactly. Even PHP and Perl added it in the 1990s, it's pretty ubiquitous at this point. Everyone decided that it was a good idea and just ran with it. – tadman Jul 18 '23 at 15:01

1 Answers1

4

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))
AKX
  • 152,115
  • 15
  • 115
  • 172
  • I appreciate for pointing the mistake. I just have one question: I thought of calling `ADS1015()` inside the method `through_ads1015()` only. In your answer, you called it outside this method as `self.adc = Adafruit_ADS1x15.ADS1015()`. Do you think it is not possible to call it inside the method directly, not outside of it? – Mainland Jul 17 '23 at 16:44
  • 1
    Of course it is possible, but [seeing as that class does additional initialization of its own](https://github.com/adafruit/Adafruit_Python_ADS1x15/blob/804728974fcefaafc8b5994be65d22e9c198a8d1/Adafruit_ADS1x15/ADS1x15.py#L75-L82) it's probably not a great idea to do that every time if you can only do it once. – AKX Jul 17 '23 at 16:51
  • Thank you very much for the nice explanation. I learned a new lesson today. – Mainland Jul 17 '23 at 16:55