• 카테고리

    질문 & 답변
  • 세부 분야

    프로그래밍 언어

  • 해결 여부

    해결됨

(메타클래스) 해당 강의 코드 혹시 올려주실 수 있을까요?

23.11.25 17:01 작성 조회수 159

1

질문은 많으시면 많을수록 좋습니다. 가능한 빠른 답변 드리겠습니다.

원활한 답변을 위해, 자세한 질문 사항 부탁드려요

 

열심히 타이핑해서 저장을 해뒀는데 실수로 삭제를 해버렸네요. 복습 겸 다시 듣고 있는데 혹시 다른 강의처럼 코드를 올려주실 수 있을까요?

답변 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

서영민님의 프로필

서영민

질문자

2023.11.26

빠른 답변 정말 감사드립니다!