Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Answer

Post Timeline

class Student(object):
 name = ""
 age = 0
 major = ""
 # theThe 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.

class Student(object):
 name = ""
 age = 0
 major = ""
 # the class "constructor" - It's actually an initializer 
 def __init__(self, name, 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.

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.

added 33 characters in body
Source Link
raam86
  • 6.9k
  • 2
  • 33
  • 46
class Student(object):
 name = ""
 age = 0
 major = ""
 # the class constructor"constructor" - It's actually an initializer 
 def __init__(self, name, 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.

class Student(object):
 name = ""
 age = 0
 major = ""
 # the class constructor
 def __init__(self, name, 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.

class Student(object):
 name = ""
 age = 0
 major = ""
 # the class "constructor" - It's actually an initializer 
 def __init__(self, name, 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.

corrected "later" to "latter", rephrased the Python philosophy bit.
Source Link
class Student(object):
 name = ""
 age = 0
 major = ""
 # the class constructor
 def __init__(self, name, 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 in Python, which tries to exposethough one of the principles in Python's philosophy that "there should be one—and preferably only one—obvious way to do it"is "there should be one—and preferably only one—obvious way to do it", there are still multiple ways to do this. You couldcan also do thisuse 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 laterlatter can be useful -- like– one being when working with document databases like MongoDB.

class Student(object):
 name = ""
 age = 0
 major = ""
 # the class constructor
 def __init__(self, name, 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 in Python, which tries to expose the philosophy that "there should be one—and preferably only one—obvious way to do it", there are still multiple ways to do this. You could also do this 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 later can be useful -- like working with document databases like MongoDB.

class Student(object):
 name = ""
 age = 0
 major = ""
 # the class constructor
 def __init__(self, name, 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.

Better style
Source Link
Wulfram
  • 3.4k
  • 2
  • 17
  • 11
Loading
Source Link
Wulfram
  • 3.4k
  • 2
  • 17
  • 11
Loading
lang-py

AltStyle によって変換されたページ (->オリジナル) /