class with subclass
Posted: 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:
ok so I try:
--> TypeError: function takes 1 positional arguments but 0 were given (This does not happen with dht, why?)
okay, then i try.
--> 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__.
--> This is info from MyBase
--> 1 (ah!)
--> 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
edit: code in codetags
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
Code: Select all
import myclass
hello1 = myclass.Hello1
hello1.getv()
okay, then i try.
Code: Select all
hello1.getv(hello1)
--> 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)
Code: Select all
hello1.getv(hello1)
Code: Select all
hello1.measure(hello1)
hello1.getv(hello1)
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
edit: code in codetags