![]() ![]() The class will not be defined until Python has executed the entirety of the definition, so you can be sure that you can reference any method from any other method on the same class, or even reference the class inside a method of the class. ![]() Defining a class also doesn’t make anything run – it just tells Python about the class. Remember that defining a function doesn’t make the function run. ![]() We don’t have to assign it to an intermediate variable before using it as a parameter to Person we can just create it when we call Person, just like we create the string literals for the other parameters. The date class is defined in the datetime module, and we create a new instance of this class to use as the birthdate parameter when we create an instance of the Person class. Note that the birthdate attribute is itself an object. The age function doesn’t take any parameters except self – it only uses information stored in the object’s attributes, and the current date (which it retrieves using the datetime module). We use the same names for the attributes and the parameters, but this is not compulsory. Now you should be able to see that our _init_ function creates attributes on the object and sets them to the values we have passed in as parameters. It doesn’t have to be called self, but this is a very strongly followed convention. In some languages this parameter is implicit – that is, it is not visible in the function signature – and we access it with a special keyword. This gives us a way to access the object’s properties from inside the object’s methods. This is because whenever we call a method on an object, the object itself is automatically passed in as the first parameter. You may have noticed that both of these method definitions have self as the first parameter, and we use this variable inside the method bodies – but we don’t appear to pass this parameter in. There is a different method called _new_ which is more analogous to a constructor, but it is hardly ever used. ![]() _init_ is sometimes called the object’s constructor, because it is used similarly to the way that constructors are used in other languages, but that is not technically correct – it’s better to call it the initialiser. The second method is a custom method which calculates the age of our person using the birthdate and the current date. The purpose of this method is thus to set up a new object using data that we have provided. When we call the class object, a new instance of the class is created, and the _init_ method on this new object is immediately executed with all the parameters that we passed to the class object. The first is called _init_, which is a special method. Inside the class body, we define two functions – these are our object’s methods. We would list any parent classes in between round brackets before the colon, but this class doesn’t have any, so we can leave them out. We start the class definition with the class keyword, followed by the class name and a colon. 12 Short Street, Greenville", "5", ) print ( person. day ): age -= 1 return age person = Person ( "Jane", "Doe", datetime. email = email def age ( self ): today = datetime. Import datetime # we will use this for date objects class Person : def _init_ ( self, name, surname, birthdate, address, telephone, email ): self. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |