Object-oriented design of Python functions

Source: Internet
Author: User
Tags ming

Understanding object-oriented design through several functional numbering evolutions

def01.py

1Dog1 = {2     'name':'hanyyyy',3     'Gender':'female',4     'type':'Tibetan Mastiff'5 }6DOG2 = {7     'name':'Lee Lee',8     'Gender':'Public',9     'type':'Sausage'Ten } One  A defJiao (dog): -     Print('a dog [%s], Barking'%dog['name']) -  the defYaoren (dog): -     Print('a dog [%s] is biting.'%dog['type']) -  - Jiao (dog1) + Yaoren (DOG1) -Yaoren (DOG2)

def02.py

6 7 defDog ():8     defJiao (dog):9         Print('a dog [%s], Barking'%dog['name'])Ten  One     defYaoren (dog): A         Print('a dog [%s] is biting.'%dog['type']) -  -Dog1 = { the         'name':'hanyyyy', -         'Gender':'female', -         'type':'Tibetan Mastiff', -         'Jiao': Jiao, +         'Yaoren': Yaoren -     } +     returnDog1 A  atD1 =Dog () -d1['Jiao'] (D1)

def03.py

6 7 defDog (name,gender,type):8     defJiao (dog):9         Print('a dog [%s], Barking'%dog['name'])Ten  One     defYaoren (dog): A         Print('a dog [%s] is biting.'%dog['type']) -  -Dog1 = { the         'name': Name, -         'Gender': Gender, -         'type': Type, -         'Jiao': Jiao,#intrinsic Functions +         'Yaoren': Yaoren#intrinsic Functions -     } +     returnDog1#because of the scope problem, be sure to return the Jiao,yaoren function with the internal return to be called externally A  atD1 = Dog ('Zhang Ming','female','Sausage')

d1['Jiao'] (D1)#introduce the returned array D1,

def04.py

6 7 defDog (name,gender,type):8     defJiao (dog):9         Print('a dog [%s], Barking'%dog['name'])Ten  One     defYaoren (dog): A         Print('a dog [%s] is biting.'%dog['type']) -  -     defInit (name,gender,type): theDog1 = { -             'name': Name, -             'Gender': Gender, -             'type': Type, +             'Jiao': Jiao,#intrinsic Functions -             'Yaoren': Yaoren#intrinsic Functions +         } A         returnDog1 at  -     returnInit (name,gender,type) -  -D1 = Dog ('Zhang Ming','female','Sausage')
Print (d1[' name '],d1[' gender ')
 d1['jiao'(d1)# introduces the returned array D1,

# So a function,
# is given a different property, set the property to its interior,
# and its internal functions use these attributes to complete the new action,
# by returning the method to a dictionary, let the external call this method (properties and methods need to be accessed externally, in the dictionary type, is equal),
# that completes the so-called "object-oriented"

# objects are nothing before they are described.
An object is an action and feature that needs to be described in order to define it. For example: People, what kind, what can do, light bulbs, what, can do what.

# classes are abstract, only properties and methods. The dog () function is
# but objects are defined by actions and features. D1 definition, it's a dog.
# This is object-oriented design

A class is like a template, which is defined as an object.

The process of producing-----objects by a class-----is called instantiation (Instance = object)

in Python, you are not required to write object-oriented code. With object-oriented language, and the design of a program is to face the object, there is no relationship between the two.

The program you write is not object-oriented, and the language you are using is not object-oriented and non-relational! Object-oriented design is just the evolution of function/process programming. The key is the object-oriented design idea.

Object-oriented design of Python functions

Related Article

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.