class Car:
def __init__(self,name,color):
self.name = name
self.color = color
def run(self):
print('%s is on the road!!!'% car.name)
car = Car('tesila','red')
car.run()
tesila is on the road!!!
class Motor(Car):
def __init__(self,name,speed):
Car.__init__(self,name,'black')
self.speed = speed
def run(self):
print('%s with %s on the road while with %s km/h!!!'% (self.name,self.color,self.speed))
motor = Motor('ha lei',100)
motor.run()
ha lei with black on the road while with 100 km/h!!!
class Hello:
def __init__(self,words):
self.words = words
hello = Hello('hello world!!')
hello
<__main__.Hello at 0x2b6990e8250>
class Hello:
def __init__(self,words):
self.words = words
def __repr__(self):
return "the word is:%s"% self.words
hello = Hello("hello world")
hello
the word is:hello world
class Numbers:
def __init__(self,value):
self.value = value
L = Numbers([10,9,8,7,6,5,4,3,2])
L.value
[10, 9, 8, 7, 6, 5, 4, 3, 2]
L[0]
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_10788/3817280154.py in <module>
----> 1 L[0]
TypeError: 'Numbers' object is not subscriptable
class Numbers:
def __init__(self,value):
self.value = value
def __getitem__(self,i):
return self.value[i]
def __setitem__(self, key, value):
self.value[key] = value
L = Numbers([10,9,8,7,6,5,4,3,2])
L[0]
10
L[0] = 100
L[0]
100
class words:
def __init__(self,name):
self.name = name
def __len__(self):
return len(self.name)
w = words("huanle")
len(w)
6
class words:
def __init__(self,name):
self.name = name
w = words("huanle")
len(w)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_10788/2910865679.py in <module>
----> 1 len(w)
TypeError: object of type 'words' has no len()
|