class with subclass

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
paulc
Posts: 6
Joined: Sun Dec 10, 2017 7:36 pm

class with subclass

Post by paulc » Mon Dec 11, 2017 2:31 pm

I'm trying to create a simple baseclass with subclasses like in dht.py (ESP8266)
The problem is: 'myclass' has a different behaviour than the dht class, and I can't figure out what is causing it.

myclass.py:

Code: Select all

class MyBase:
    def __init__(self):
        self.temp = 1
        self.printinfo(self)

    def measure(self):
        self.temp = 23
    def printinfo(self):
        print("This is info from MyBase")

class Hello1(MyBase):
    def printinfo1(self):
        print("This is info from Hello1 ")

    def getv(self):
        t = self.temp
        return t

class Hello2(MyBase):
    def printinfo1(self):
        print("This is info from Hello2")

    def getv(self):
        t = "Hello2"
        return t
ok so I try:

Code: Select all

import myclass
hello1 = myclass.Hello1
hello1.getv()
--> TypeError: function takes 1 positional arguments but 0 were given (This does not happen with dht, why?)
okay, then i try.

Code: Select all

hello1.getv(hello1)
--> File "myclass.py", line 17, in getv
--> AttributeError: type object 'Hello1' has no attribute 'temp' (apparantly it dit not run __init__??, why?)
okay, so i need to manually run __init__.

Code: Select all

hello1.__init__(hello1)
--> This is info from MyBase

Code: Select all

hello1.getv(hello1)
--> 1 (ah!)

Code: Select all

hello1.measure(hello1)
hello1.getv(hello1)
--> 23 (as expected)

So what is the essential difference between the dht class and 'myclass'?, or what am I doing wrong?
This has to be a simple beginners question :oops:

edit: code in codetags
Last edited by paulc on Wed Dec 13, 2017 9:14 pm, edited 2 times in total.

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: class with subclass

Post by deshipu » Tue Dec 12, 2017 6:45 pm

If you are calling a function on the class directly, it's just a normal function, you if it takes a "self" parameter, you have to provide it. The method magic where the current instance is assigned to the fist "self" parameter of the function only happens when you call it on the *instance* of a class.

Code: Select all

> import myclass
> hello1 = myclass.Hello1()
> hello1.getv()

paulc
Posts: 6
Joined: Sun Dec 10, 2017 7:36 pm

Re: class with subclass

Post by paulc » Wed Dec 13, 2017 9:13 pm

Thank you very much for your reply.
I would never have gotten into trouble if my class would have had parameters.;-)
I guess I sort of understand the difference now. 'copy' or 'instance'

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: class with subclass

Post by deshipu » Wed Dec 13, 2017 11:35 pm

It's not even a copy, it points to the same class in memory.

paulc
Posts: 6
Joined: Sun Dec 10, 2017 7:36 pm

Re: class with subclass

Post by paulc » Mon Dec 18, 2017 7:54 pm

It actually is the same object indeed:

Code: Select all

hello1 = myclass.Hello1
Hello1.measure(Hello1)
hello1.getv(hello1)
result: 23!
Last edited by paulc on Tue Dec 19, 2017 8:03 am, edited 1 time in total.

SpotlightKid
Posts: 463
Joined: Wed Apr 08, 2015 5:19 am

Re: class with subclass

Post by SpotlightKid » Mon Dec 18, 2017 10:39 pm

I don't think you've understood the difference between a class and an instance of it yet.

Maybe you should work through a Python language tutorial on object-orientation?

Post Reply