Class is a blueprint to create an Object (Instance)
소스코드를 최소화 하면서 현실 세계 사물을 컴퓨터 상에서 쉽게 표현할 수 있도록 해줌
class User:
pass
user_1 = User()
Attribute: variable that's associated with an object. attached to an object
- part of a blueprint, that allows us to specify what should happen when our object is being constructed.
- initializing an object. we can set variables to their starting values
this will be called everytime you created a new object from this class.
-The init function is called a constructor, or initializer, and is automatically called when you create a new instance of a class. Within that function, the newly created object is assigned to the parameter self .
class Car:
def __init__(self, seats):
self.seats = seats
my_car = car(5)
my_car.seats = 5
class User:
def __init__(self, user_id, user_name):
self.id = user_id
self.user_name = user_name
#-> convention: name of the parameter sames as name of attribute
self.follower = 0 #default
user_1 = User('001', 'angela')
user_2 = User('002', 'jack')
print(user_1.id) #-> 001
print(user_1.follower) #-> 0
can add Parameters, its going to be passed in when an obj gets contructed from this class.
whenever you create a new object, you need to provide arguments.
class User:
pass
user_1 = User()
user_1.id = "001"
user_2 = User()
user_2.id = "002"
class Car:
def enter_race_mode():
self.seats = 2
gets hold of the object (self), seats attritube changed to 2
class User:
def __init__(self, user_id, user_name):
self.id = user_id
self.user_name = user_name
self.followers = 0
self.following = 0
def follow(self, user):
user.followers += 1
self.following += 1
user1.follow(user_2)
- dir() function :returns list of the attributes and methods of any object
- self : self represents the instance of the class. By using the “self” keyword we can access the attributes and methods of the class in python.
Why are we taking data by using Class? -> We have data in a seperate file, which are dictionary form in a list, it can be specified with the 'key'(string).
"we are converting each pieces of data into an object." In this way, it provides easy and foolproof way of accessing the data with preset attributes.
e.g. question_bank[10].text
- question_bank was an empty list, we appended each object into it. Hence, we can access data simply by using '.text' instead of saying 'question_data["text"]'
- For loop: My first attempt was accessing it with location numbers; however, for loop doesn't need it as it automatically access each items as a whole, valid item. (in this case, each item was a long dictionary, however long it is, it is taken as ONE ITEM in a list). I think this habit is originated from using while loop.
- Angela's code is using bunch of variables. Basically same functionality as mine.
#-------------------my code ----------------------------------
question_bank = []
q_test = Question(question_data[item]["text"], question_data[item]["answer"])
print(q_test.text)
print(q_test.answer)
print(len(question_data))
for item in range(len(question_data)):
question_bank.append(Question(question_data[item]["text"], question_data[item]["answer"]))
#-------------------angela's code ----------------------------------
for question in question_data:
question_text = question["text"]
question_answer = question["answer"]
new_question = Question(question_text, question_answer)
question_bank.append(new_question)
print(question_bank) #-> it prints out the address of objects.
print(question_bank[10].text)
print(question_bank[0].answer)