class Student(object):
name = ""
age = 0
major = ""
# The class "constructor" - It's actually an initializer
def __init__(self, name, age, major):
self.name = name
self.age = age
self.major = major
def make_student(name, age, major):
student = Student(name, age, major)
return student
Note that even though one of the principles in Python's philosophy is "there should be one—and preferably only one—obvious way to do it", there are still multiple ways to do this. You can also use the two following snippets of code to take advantage of Python's dynamic capabilities:
class Student(object):
name = ""
age = 0
major = ""
def make_student(name, age, major):
student = Student()
student.name = name
student.age = age
student.major = major
# Note: I didn't need to create a variable in the class definition before doing this.
student.gpa = float(4.0)
return student
I prefer the former, but there are instances where the latter can be useful – one being when working with document databases like MongoDB.
Answer from Wulfram on Stack Overflowclass Student(object):
name = ""
age = 0
major = ""
# The class "constructor" - It's actually an initializer
def __init__(self, name, age, major):
self.name = name
self.age = age
self.major = major
def make_student(name, age, major):
student = Student(name, age, major)
return student
Note that even though one of the principles in Python's philosophy is "there should be one—and preferably only one—obvious way to do it", there are still multiple ways to do this. You can also use the two following snippets of code to take advantage of Python's dynamic capabilities:
class Student(object):
name = ""
age = 0
major = ""
def make_student(name, age, major):
student = Student()
student.name = name
student.age = age
student.major = major
# Note: I didn't need to create a variable in the class definition before doing this.
student.gpa = float(4.0)
return student
I prefer the former, but there are instances where the latter can be useful – one being when working with document databases like MongoDB.
Create a class and give it an __init__ method:
class Student:
def __init__(self, name, age, major):
self.name = name
self.age = age
self.major = major
def is_old(self):
return self.age > 100
Now, you can initialize an instance of the Student class:
>>> s = Student('John', 88, None)
>>> s.name
'John'
>>> s.age
88
Although I'm not sure why you need a make_student student function if it does the same thing as Student.__init__.
Videos
Basically I'm trying to make many objects without having to define each of them manually, and be able to use methods on specific ones later. I've tried this, from how I understood it on Stack Overflow
class clas:
def __init__(self):
self.name="obj{}".format(i)
#someothercode
def some_method(self):
print(self.name)
#someothercode
dict={}
for i in range(1,6):
dict[i]=clas()But when I try to use specific objects with something like "obj2.some_method()" later, it tells me they are not defined. I can only use that method in the for loop or in the init. I have also tried using __repr__, with the same result. I also tried using id, but it didn't help, although maybe I didn't really understand it. I already understand that neither repr nor name actually name objects, but I cannot find or maybe understand what I should be doing instead. I have tried to search for it several times, but all the answers seem to help only with attributes, not things you can refer to.
From what I understand....
Class - is basically the blueprint from which you create objects. This is where you state what attributes the item will have (e.g. name, colour) but you wont actually assign the value of the attribute (e.g. iphone, red).
Object - is basically the values (e.g. the actual name/colour of the item) to the attributes/properties you chose the item to have
Is that right? If possible, if you have anything to add. I would really appreciate if you explain to me in an easy to understand way. Im new to python and computers as a whole but slowly learning
Thankyou in advance