Python, creating objects

前端 未结 4 998
旧巷少年郎
旧巷少年郎 2020-12-04 07:04

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 pro

相关标签:
4条回答
  • 2020-12-04 07:39

    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_student function by explicitly assigning the attributes to a new instance of 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__) -

    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).

    If you use that, then a make_student function is trivial (and superfluous) -

    def make_student(name, age, major):
        return Student(name, age, major)
    

    For fun, here is an example of how to create a make_student function without defining a class. Please do not try this at home.

    def make_student(name, age, major):
        return type('Student', (object,),
                    {'name': name, 'age': age, 'major': major})()
    
    0 讨论(0)
  • 2020-12-04 07:41
    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.

    0 讨论(0)
  • 2020-12-04 07:48

    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 init method 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..

    class Student:
          def __init__(self,name,age):
                self.name=value
                self.age=value
    
     # creating an object.......
    
         student2=Student("smith",25)
    
    0 讨论(0)
  • 2020-12-04 08:03

    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__.

    0 讨论(0)
提交回复
热议问题