Python notes (8)

Source: Internet
Author: User

Object-oriented nature of Python

Class and object are two main aspects of object-oriented programming. Class to create a new type, and the object classInstance. This is similarintType variable, which stores the integer variable isintClass instance (object ).

 

In python, even integers are used as objects (intClass ). This is different from C ++ and Java (Before Version 1.5) that take integers purely as types. Passhelp(int)Learn more about this class.

 

Objects can use common variables that belong to objects to store data. A variable of an object or class is called a field. Objects can also use functions of classes. Such a function is called a class method. These terms help us distinguish them from isolated functions and variables. Fields and methods can be collectively called class attributes.

 

1. Python's _ init _ Method

For example:

#! /Usr/bin/python <br/> # filename: class_init.py </P> <p> class person: <br/> def _ init _ (self, name ): <br/> self. name = Name <br/> def sayhi (Self): <br/> Print 'hello, my name is ', self. name </P> <p> P = person ('swaroop ') <br/> P. sayhi () </P> <p> # This short example can also be written as person ('swaroop '). sayhi () </P> <p>

Output:

$ Python class_init.py <br/> hello, my name is swaroop </P> <p>

To a certain extent, it can be considered as a constructor in Python.

Like__init__The same method, there is a special method__del__, Similar to destructor.

 

Note: All class members (including data members) in Python are public and all methods are valid.
Only one exception: if the data member name you use usesDouble underline prefixFor example__privatevarThe name Management System of Python effectively uses it as a private variable.
In this way, there is a convention that if a variable only needs to be used in a class or object, it should be prefixed with a single underscore. Other names are public and can be used by other classes/objects. Remember that this is just a convention and is not required by Python (different from the double-underline prefix ).

 

 

 

 

Ii. Inheritance of Classes

#! /Usr/bin/python <br/> # filename: Inherit. PY </P> <p> class schoolmember: <br/> ''' represents any school member. ''' <br/> def _ init _ (self, name, age): <br/> self. name = Name <br/> self. age = age <br/> Print '(initialized schoolmember: % s)' % self. name </P> <p> def tell (Self): <br/> ''' tell my details. ''' <br/> Print 'name: "% s" Age: "% s" '% (self. name, self. age), </P> <p> class teacher (schoolmember): <br/> '''represents a teacher. ''' <br/> def _ init _ (self, name, age, salary): <br/> schoolmember. _ init _ (self, name, age) <br/> self. salary = salary <br/> Print '(initialized Teacher: % s)' % self. name </P> <p> def tell (Self): <br/> schoolmember. tell (Self) <br/> Print 'salary: "% d" '% self. salary </P> <p> class student (schoolmember): <br/> '''represents a student. ''' <br/> def _ init _ (self, name, age, marks): <br/> schoolmember. _ init _ (self, name, age) <br/> self. marks = marks <br/> Print '(initialized Student: % s)' % self. name </P> <p> def tell (Self): <br/> schoolmember. tell (Self) <br/> Print 'Marks: "% d" '% self. marks </P> <p> T = teacher ('Mrs. shrividya', 40,300 00) <br/> S = student ('swaroop ', 22, 75) </P> <p> Print # prints a blank line </P> <p> members = [t, s] <br/> for member in members: <br/> member. tell () # works for both teachers and students </P> <p>

 

Output:

 

$ Python inherit. PY <br/> (initialized schoolmember: Mrs. shrividya) <br/> (initialized Teacher: Mrs. shrividya) <br/> (initialized schoolmember: swaroop) <br/> (initialized Student: swaroop) </P> <p> name: "mrs. shrividya "Age:" 40 "Salary:" 30000 "<br/> name:" swaroop "Age:" 22 "marks:" 75 "</P> <p>

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.