OOP concept in Python - Part 1
Classes and Instances
Classes and instances are related to each other: classes provide the definition of an object, and instances are "the real McCoy," the objects specified in the class definition brought to life.
Here is an example of how to create a class:
class MyNewObjectType(bases):
'define MyNewObjectType class'
class_suite
The keyword is class, followed by the class name. What follows is the suite of code that defines the class. This usually consists of various definitions and declarations. The biggest difference between declaring new-style classes and classic classes is that all new-style classes must inherit from at least one parent class. The bases argument is one (single inheritance) or more (multiple inheritance) parent classes to derive from.
The "mother of all classes" is object. If you do not have any ancestor classes to inherit from, use object as your default. It must exist at the top of every class hierarchy. If you do not subclass object or a class that subclasses object, then you have defined a classic class:
class MyNewObjectType:
'define MyNewObjectType classic class'
class_suite
Conversely, if you do not specify a parent class, or if you subclass a base class without a parent class, you have created a classic class. Most Python classes are still classic classes. There really is no problem with using them until they become obsolete in some future version of Python. We do recommend that you use new-style classes whenever possible, but for learning purposes, either type will suffice.
The process of creating an instance is called instantiation, and it is carried out like this (note the conspicuous absence of a new keyword):
myFirstObject = MyNewObjectType()
The class name is given as an "invocation," using the familiar function operators ( ( ) ). You then typically assign that newly created instance to a variable. The assignment is not required syntactically, but if you do not save your instance to a variable, it will be of no use and will be automatically garbagecollected because there would no references to that instance. What you would be doing is allocating memory, then immediately deallocating it.
Thank you....:)
thank you.
ReplyDeletepython3 tutorial
java tutorial