I"m trying to learn python and I now I am trying to get the hang of classes and how to manipulate them with instances.
I can"t seem to understand this practice problem:
Create and return a student object whose name, age, and major are the same as those given as input
def make_student(name, age, major)
I just don"t get what it means by object, do they mean I should create an array inside the function that holds these values? or create a class and let this function be inside it, and assign instances? (before this question i was asked to set up a student class with name, age, and major inside)
class Student: name = "Unknown name" age = 0 major = "Unknown major"
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.
Systems programming provides the basis for global calculation. Developing performance-sensitive code requires a programming language that allows programmers to control the use of memory, processor tim...
The Apache Hadoop software library has come into it’s own. It is the basis for advanced distributed development for a host of companies, government institutions, and scientific research facilities. ...
Spark is one of the hottest technologies in big data analysis right now, and with good reason. If you work for, or you hope to work for, a company that has massive amounts of data to analyze, Spark of...
Managing and analyzing data have always offered the greatest benefits and the greatest challenges for organizations of all sizes and across all industries. Businesses have long struggled with finding ...