Missing one positional required argument `self`

huangapple go评论57阅读模式
英文:

Missing one positional required argument `self`

问题

我正在尝试使用面向对象编程(OOP)开发一个函数。但我遇到了以下错误。该函数主要用于树莓派。在没有任何OOP结构的情况下使用它时,它一直运行正常。

代码:

import Adafruit_ADS1x15 as ada_adc

class read_raspberrypi_analog_input:
    ## 默认级别

    # 当调用self时执行以下操作
    def __init__(self):
        # 如果用于读取模拟输入信号,则调用ADS1015的一个实例
        self.adc = ada_adc

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

        GAIN = adc_gain

        # 读取板上输入模拟引脚A0处的值
        a0_level = adc.read_adc(0, gain=GAIN)
        print(a0_level)
        a0_analog = a0_level * (4.096 / 2047)
        print(a0_analog)
        # 实际传感器输入电压
        r1 = r1
        r2 = r2
        a0_sensor = a0_analog * (r1 + r2) / r1
        print(a0_sensor)

# 调用类和方法:
read_raspberrypi_analog_input().through_ads1015(adc_gain=1, r1=5.1, r2=3)

现在的输出:

1357
2.685629299780289
8.362442396313364

请注意,我已经将代码中的问题修复,包括在__init__方法中添加self参数,并使用read_raspberrypi_analog_input()来创建类的实例。

英文:

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'

答案1

得分: 4

你根本没有实例化你的类,你试图在类本身上调用一个方法,如果你没有明确传入 self,那么你就缺少了这个位置参数。

很有可能你想要在构造包装对象时仅初始化 ADC 对象一次,就像这样 - 在你的原始代码中,你只是存储了模块的引用。

import Adafruit_ADS1x15

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

    def through_ads1015(self, adc_gain=1, r1=1, r2=0):
        # 读取模拟引脚 A0 上的值
        a0_level = self.adc.read_adc(0, gain=adc_gain)
        print(a0_level)
        a0_analog = a0_level * (4.096 / 2047)
        print(a0_analog)
        # 实际传感器输入电压
        a0_sensor = a0_analog * (r1 + r2) / r1
        print(a0_sensor)

ai = ReadRaspberryPiAnalogInput()
print(ai.through_ads1015(adc_gain=1, r1=5.1, r2=3))
英文:

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))

huangapple
  • 本文由 发表于 2023年7月18日 00:33:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76706471.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定