python - what is the difference between is-a and has-a? - Stack Overflow
Python has been an object-oriented language since it existed. .. You can use issubclass() or isinstance() functions to check a relationships of two classes and. In knowledge representation, object-oriented programming and design is-a (is_a or is a) is a subsumption relationship between. The technical term for this is "Inheritance" ("is-a") versus "Composition" ("has-a"). Or you might build interfaces that the car HAS rather than different kinds of.
Encapsulation is a good idea for several reasons: We could switch to using another object which is completely different on the inside, and not have to change any code because both objects have the same interface. Some languages have features which allow us to enforce encapsulation strictly. In Java it is also considered good practice to write setters and getters for all attributes, even if the getter simply retrieves the attribute and the setter just assigns it the value of the parameter which you pass in.
It is also customary to set and get simple attribute values directly, and only write setter and getter methods for values which require some kind of calculation.
In Python, there are two main types of relationships between classes: We saw in the previous chapter how we can make a datetime. We can say that a person has a birthdate — if we can express a relationship between two classes using the phrase has-a, it is a composition relationship.
Relationships like this can be one-to-one, one-to-many or many-to-many, and they can be unidirectional or bidirectional, depending on the specifics of the the roles which the objects fulfil. According to some formal definitions the term composition implies that the two objects are quite strongly linked — one object can be thought of as belonging exclusively to the other object.
If the owner object ceases to exist, the owned object will probably cease to exist as well. If the link between two objects is weaker, and neither object has exclusive ownership of the other, it can also be called aggregation.
Here are four classes which show several examples of aggregation and composition: This is an example of the way that translation of real-life concepts into objects in your code may not always be as straightforward as it appears. Would it have made sense to have a single course object which has both description, code and department attributes and a list of students?
We have represented these two concepts by two separate classes which are linked to each other. Course is the theoretical description of a course, and CourseRunning is the concrete instance of a course.
We have defined several relationships between these classes: A student can be enrolled in several courses CourseRunning objectsand a course CourseRunning can have multiple students enrolled in it in a particular year, so this is a many-to-many relationship.
A student knows about all his or her courses, and a course has a record of all enrolled students, so this is a bidirectional relationship. A department offers multiple courses Course objectsbut in our implementation a course can only have a single department — this is a one-to-many relationship. It is also bidirectional.
Furthermore, these objects are more strongly coupled — you can say that a department owns a course. The course cannot exist without the department. You should include classes for songs, artists, albums and playlists.
Python Tutorial: Inheritance
You can also assume that each song is associated with a single album, but that multiple copies of the same song which are included in different albums can exist. Write a simple implementation of this model which clearly shows how the different classes are composed. Write some example code to show how you would use your classes to create an album and add all its songs to a playlist. An object which inherits from another object is considered to be a subtype of that object.
As we saw in the previous chapter, all objects in Python inherit from object. We can say that a string, an integer or a Person instance is an object instance. When we can describe the relationship between two objects using the phrase is-a, that relationship is inheritance. We also often say that a class is a subclass or child class of a class from which it inherits, or that the other class is its superclass or parent class.
We can refer to the most generic class at the base of a hierarchy as a base class. Inheritance can help us to represent objects which have some differences and some similarities in the way they work. We can put all the functionality that the objects have in common in a base class, and then define one or more subclasses with their own custom functionality.
Inheritance is also a way of reusing existing code easily. If we already have a class which does almost what we want, we can create a subclass in which we partially override some of its behaviour, or perhaps add some new functionality.
Here is a simple example of inheritance: We create a subclass to represent students and one to represent staff members, and then a subclass of StaffMember for people who teach courses as opposed to staff members who have administrative positions. We represent both student numbers and staff numbers by a single attribute, number, which we define in the base class, because it makes sense for us to treat them as a unified form of identification for any person.
We use different attributes for the kind of student undergraduate or postgraduate that someone is and whether a staff member is a permanent or a temporary employee, because these are different sets of options. We have also added a method to Student for enrolling a student in a course, and a method to Lecturer for assigning a course to be taught by a lecturer. Because we use the class of an exception to determine whether it should be caught by a particular except block, it is useful for us to define custom classes for exceptions which we want to raise in our code.
Using inheritance in our classes is useful because if an except block catches a particular exception class, it will also catch its child classes because a child class is its parent class. That means that we can efficiently write except blocks which handle groups of related exceptions, just by arranging them in a logical hierarchy. Write a simple program which loops over a list of user data tuples containing a username, email and age and adds each user to a directory if the user is at least 16 years old.
You do not need to store the age. Write a simple exception hierarchy which defines a different exception for each of these error conditions: Whenever an exception occurs, your program should move onto the next set of data in the list. Print a different error message for each different kind of exception. Think about where else it would be a good idea to use a custom class, and what kind of collection type would be most appropriate for your directory.
You can assume that the age is already an integer value. At a real university, the divisions between staff and students and administrative and teaching staff are not always clear-cut. A student who tutors a course is also a kind of temporary staff member. A staff member can enrol in a course. A staff member can have both an administrative role in the department and a teaching position.
In Python it is possible for a class to inherit from multiple other classes. The operation performed varies by the types of objects or arguments involved. An object obj that belongs to a class Circle, for example, is an instance of the class Circle.
An object comprises both data members class variables and instance variables and methods. Creating Classes The class statement creates a new class definition. This can be accessed as Employee. You declare other class methods like normal functions with the exception that the first argument to each method is self. Python adds the self argument to the list for you; you do not need to include it when you call the methods.
If attribute does not exist, then it would be created.
Common base class for all employees Employee. The process by which Python periodically reclaims blocks of memory that no longer are in use is termed Garbage Collection. Python's garbage collector runs during program execution and is triggered when an object's reference count reaches zero.
An object's reference count changes as the number of aliases that point to it changes. An object's reference count increases when it is assigned a new name or placed in a container list, tuple, or dictionary.
The object's reference count decreases when it's deleted with del, its reference is reassigned, or its reference goes out of scope. When an object's reference count reaches zero, Python collects it automatically.