21
loading...
This website collects cookies to deliver better user experience
name = "Mike"
print(type(name)) # <class 'str'>
class Fruit:
def __init__(self, color, shape):
self.color = color
self.shape = shape
def show_characteristics(self):
characteristics = [self.shape, self.color]
return characteristics
class Fruit:
def __init__(self, color, shape):
self.color = color
self.shape = shape
def show_characteristics(self):
characteristics = [self.shape, self.color]
return characteristics
print(type(Fruit))
It is worth noting that type takes either 1 or 3 arguments. When a single argument is passed, it will return the class from which the object inherits. When 3 arguments are passed, it returns to you a new class with the characteristics and attributes you pass in as last argument. More on that below
def fruit_init(self, shape, color):
self.shape = shape
self.color = color
def show_characteristics(self):
characteristics = [self.color, self.shape]
return characteristics
fruit_class = type("FruitClass", (), {"__init__": fruit_init, "show_characteristics": show_characteristics})
def fruit_init(self, shape, color):
self.shape = shape
self.color = color
def show_characteristics(self):
characteristics = [self.color, self.shape]
return characteristics
fruit_class = type("FruitClass", (), {
"__init__": fruit_init, "show_characteristics": show_characteristics})
orange = fruit_class("spherical", "orange")
print(orange.show_characteristics())