Python,创建对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15081542/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python, creating objects
提问by Mohsen M. Alrasheed
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.
我正在尝试学习 python,现在我正在尝试掌握类的窍门以及如何使用实例来操作它们。
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"
采纳答案by Wulfram
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:
请注意,尽管 Python 哲学中的原则之一是“应该有一种——最好只有一种——明显的方法来做到这一点”,但仍然有多种方法可以做到这一点。您还可以使用以下两个代码片段来利用 Python 的动态功能:
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.
我更喜欢前者,但在某些情况下后者可能会很有用 - 一个是在使用 MongoDB 等文档数据库时。
回答by Blender
Create a class and give it an __init__method:
创建一个类并给它一个__init__方法:
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 Studentclass:
现在,您可以初始化Student该类的实例:
>>> s = Student('John', 88, None)
>>> s.name
'John'
>>> s.age
88
Although I'm not sure why you need a make_studentstudent function if it does the same thing as Student.__init__.
虽然我不确定为什么你需要一个make_student学生功能,如果它和Student.__init__.
回答by pyrospade
Objects are instances of classes. Classes are just the blueprints for objects. So given your class definition -
对象是类的实例。类只是对象的蓝图。因此,鉴于您的类定义-
# Note the added (object) - this is the preferred way of creating new classes
class Student(object):
name = "Unknown name"
age = 0
major = "Unknown major"
You can create a make_studentfunction by explicitly assigning the attributes to a new instance of Student-
您可以make_student通过将属性显式分配给以下的新实例来创建函数Student-
def make_student(name, age, major):
student = Student()
student.name = name
student.age = age
student.major = major
return student
But it probably makes more sense to do this in a constructor (__init__) -
但在构造函数 ( __init__) 中执行此操作可能更有意义-
class Student(object):
def __init__(self, name="Unknown name", age=0, major="Unknown major"):
self.name = name
self.age = age
self.major = major
The constructor is called when you use Student(). It will take the arguments defined in the __init__method. The constructor signature would now essentially be Student(name, age, major).
使用时会调用构造函数Student()。它将采用__init__方法中定义的参数。构造函数签名现在基本上是Student(name, age, major).
If you use that, then a make_studentfunction is trivial (and superfluous) -
如果你使用它,那么一个make_student函数是微不足道的(而且是多余的)——
def make_student(name, age, major):
return Student(name, age, major)
For fun, here is an example of how to create a make_studentfunction without defining a class. Please do not try this at home.
为了好玩,这里有一个示例,说明如何在make_student不定义类的情况下创建函数。请不要在家里尝试这个。
def make_student(name, age, major):
return type('Student', (object,),
{'name': name, 'age': age, 'major': major})()
回答by GT_hash
when you create an object using predefine class, at first you want to create a variable for storing that object. Then you can create object and store variable that you created.
使用预定义类创建对象时,首先要创建一个变量来存储该对象。然后您可以创建对象并存储您创建的变量。
class Student:
def __init__(self):
# creating an object....
student1=Student()
Actually this initmethod is the constructor of class.you can initialize that method using some attributes.. In that point , when you creating an object , you will have to pass some values for particular attributes..
实际上这个init方法是类的构造函数。你可以使用一些属性来初始化那个方法。在这一点上,当你创建一个对象时,你将不得不为特定的属性传递一些值。
class Student:
def __init__(self,name,age):
self.name=value
self.age=value
# creating an object.......
student2=Student("smith",25)

