해결된 질문
작성
·
231
답변 3
1
안녕하세요 서영민님,
제가 레플릿 설정을 보니 모든 강의가 공개로 되어있었네요. 필요하시다면 https://replit.com/@SeungjoonLee4에서 모든 코드 접근하셔서 복사하시면 될 듯 합니다.
1
안녕하세요 서영민님,
코드 밑에 복사해뒀습니다. 타 강의와는 달리 강의들이 Replit에 있어서 공유하기가 힘든 면이 있는데, 시간을 내서 Github으로 옮겨보도록 하겠습니다. 의견 감사드립니다!
# Metaclasses are just classes that create other classes
# Classes are essentially just building blocks of code that tell you how to produce an object. We can then instantiate each object to create unique instances of that class.
# class Car(object):
# def __init__(self, brand, color):
# self.brand = brand
# self.color = color
# def __repr__(self): # special method
# return f"Brand: {self.brand}, Color: {self.color}"
# myTesla = Car("Tesla", "White")
# # print(myTesla)
# # Brand: Tesla, Color: White
# # Python
# # instances of the classes is consdered objects, but also class is considered object
# myTesla = Car("Tesla", "White")
# print(type(myTesla))
# # <type 'instance'>
# print(type(Car))
# # <type 'classobj'>
# # dynamically created Class
# EVCar = type('Car', (), {})
# print(EVCar())
# # # # # Name — Name of the class
# # # # # Bases — Any Classes we inherit from
# # # # # Attrs — Any Attributes associated with the same class.
# # # # # type(name, bases, attrs)
# # Create Basic Class of Car
# class Car(object):
# def __init__(self, brand, color):
# self.brand = brand
# self.color = color
# def __repr__(self):
# return f"Brand: {self.brand}, Color: {self.color}"
# # Create additional method for our new Class
# def charge(self):
# return "Charging up"
# # Create Class EVCar, inheriting from Car class with an extra attribute (batter_cap) and method (charge)
# EVCar = type('EVCar', (Car, ), {"batter_cap": "75KW", "charge": charge})
# print(EVCar)
# # Create Instance of EVCar called 'lucid'
# myLucid = EVCar('Lucid', 'Yellow')
# print(myLucid.brand)
# print(myLucid.color)
# print(myLucid.charge())
# # We can leverage type to dynamically create our other classes.
# class Meta(type):
# # __new__ is a magic method that gets called before __init__ that determines how the class is constructed.
# def __new__(self, name, bases, attrs):
# return type(name, bases, attrs)
# Car = Meta('Car', (), {})
# print(Car)
class Car(object):
def __init__(self, brand, color):
self.brand = brand
self.color = color
def __repr__(self):
return f"Brand: {self.brand}, Color: {self.color}"
# Create our Metclass
class Meta(type):
def __new__(self, name, attrs):
attrs['shape'] = 'sendan'
# Return the Class and automatically inherit from Car.
return type(name, (Car, ), attrs)
# Create the EVCar class
EVCar = Meta('EVCar', {})
# # Create Instance of EVCar class
lucid = EVCar('Lucid', 'Yellow')
print(lucid)
print(lucid.shape)
0