Class method return none why ?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
giants
Posts: 44
Joined: Fri Apr 26, 2019 2:07 pm

Class method return none why ?

Post by giants » Thu Feb 11, 2021 6:07 pm

Hi there!
i start to wrote my fist class but i got a problem. I post my code here

this is my class: ST7735R.py

Code: Select all

width = 0 
height = 0 
 
class ST7735R:
    
  def __init__(self, w, h):  
      self.width = w
      self.height = h
      
      
  def stampa(self):
      return self.width
in the main.py i wrote :

Code: Select all

from ST7735R import ST7735R 

dspl = ST7735R(128,64)

value = dspl.stampa()
print('value = {}'.format(value))
so the value return None and i don't understand why :roll: :roll:

Please some one can help me ?

Thank you

Sergio

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Class method return none why ?

Post by pythoncoder » Thu Feb 11, 2021 7:52 pm

It works here. I wonder if you were somehow importing a different version of the file. What hardware are you using?
Peter Hinch
Index to my micropython libraries.

giants
Posts: 44
Joined: Fri Apr 26, 2019 2:07 pm

Re: Class method return none why ?

Post by giants » Thu Feb 11, 2021 8:07 pm

I use an ESP32 ESP-32S ESP-WROOM-32

and i use this firmware GENERIC : esp32-idf3-20210202-v1.14.bin

Code: Select all

def stampa(self):
      print("ciao")
      return width
i try to put one print and the print work but still return None :roll: :roll: :roll:

kevinkk525
Posts: 969
Joined: Sat Feb 03, 2018 7:02 pm

Re: Class method return none why ?

Post by kevinkk525 » Thu Feb 11, 2021 8:09 pm

This is not the same code as above and you return "width" which is not in the class. You need to return "self.width" as in your first post.
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

giants
Posts: 44
Joined: Fri Apr 26, 2019 2:07 pm

Re: Class method return none why ?

Post by giants » Thu Feb 11, 2021 10:41 pm

i did but still dosen't work i really don't understand

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Class method return none why ?

Post by jimmo » Fri Feb 12, 2021 2:15 am

Can you paste the exact code you're using?

Probably the best way to test this is at the REPL.

Can you show the output of the following commands:

>>> print(open('ST7735R.py', 'r').read())

>>> print(ST7735R(128, 64).stampa())

giants
Posts: 44
Joined: Fri Apr 26, 2019 2:07 pm

Re: Class method return none why ?

Post by giants » Fri Feb 12, 2021 8:02 am

i just try with REPL and my code works, but still dosen't work on ESP32, i don't know what to think is the ESP32? is the firmware ? really i don't understand

:cry: :cry: :cry: :cry:

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Class method return none why ?

Post by pythoncoder » Fri Feb 12, 2021 9:58 am

Can I suggest you try again. Create a file foo.py with this contents:

Code: Select all

class ST7735R:
    
  def __init__(self, w, h):  
      self.width = w
      self.height = h
      
      
  def stampa(self):
      return self.width

dspl = ST7735R(128,64)

value = dspl.stampa()
print('value = {}'.format(value))
Copy it to your ESP32. Then, at the REPL, issue

Code: Select all

>>> import foo
value = 128
>>>  
It works here on an ESP32.
Peter Hinch
Index to my micropython libraries.

giants
Posts: 44
Joined: Fri Apr 26, 2019 2:07 pm

Re: Class method return none why ?

Post by giants » Fri Feb 12, 2021 11:33 am

Soon i can i try
Thank you

giants
Posts: 44
Joined: Fri Apr 26, 2019 2:07 pm

Re: Class method return none why ?

Post by giants » Fri Feb 12, 2021 4:09 pm

:D :D :D :D WORK please can you tell me why ?

Thank you

Sergio

Post Reply