Python, creating objects

前端 未结 4 1023
旧巷少年郎
旧巷少年郎 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: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)
    

提交回复
热议问题