python - class

class

  • An object made by a class template is called an instance of that class.
  • A class is a template for making an instance, and the instance reflects the structure provided by the template, the class.
  • It is important to remember that the class defines the structure and operations of an instance but that those operations are operations for class instances, not the class itself.


summary

Classes

  • Classes are objects.
  • Object-oriented programming (OOP) has three concepts: encapsulation, inheritance,
    polymorphism.
  • Creating a new class creates a new type.
  • A class is to its instance as a cookie cutter is to a cookie.
  • Classes have attributes and methods that act on those attributes. r Attributes are usually created within the init method.
  • The identifier self refers to the current instance.
  • The first parameter of methods is self.
  • Class structure
class ClassName(object):
      def init (self,param1=4):
            self.att = param1 # create attribute. 
      def str (self):
            return "some string" # return a string for printing 
      def some method(self,param):
            # do something
  • Overloading operators:
    A + B maps to A.add (B) which maps to add (self,param) by mapping A to self and mapping B to param.
  • Inheritance: inherit properties of the superclass

simple student class

class Student(object):
    def __init__(self, first='', last='', id=0):  # initializer
        self.first_name_str = first
        self.last_name_str = last
        self.id_int = id

    def __str__(self):  # string representation, for printing
        return "{} {}, ID:{}".format \
            (self.first_name_str, self.last_name_str, self.id_int)
stu1 = Student('terry', 'jones', 333)
print(stu1)

object-oriented programming (OOP)

Object-oriented programming is a powerful development tool. It is particularly supportive of the divide-and-conquer style of problem solving. If you can organize your design thoughts around OOP, you can design large programs around basic objects.
The concept of self is important in understanding how class objects work, especially in the creation and use of instances. Reread this chapter if you feel that you have not yet grasped the concept of self in Python classes.

import math


class Point(object):
    def __init__(self, x_param=0.0, y_param=0.0):
        self.x = x_param
        self.y = y_param

    def distance(self, param_pt):
        x_diff = self.x + param_pt.x
        y_diff = self.y + param_pt.y
        return math.sqrt(x_diff ** 2 + y_diff ** 2)

    def sum(self, param_pt):
        newpt = Point()
        newpt.x = self.x + param_pt.x
        newpt.y = self.y + param_pt.y
        return newpt

    def __str__(self):
        print("called the __str__ method")
        return "({:.2f},{:.2f})".format(self.x, self.y)
class NewClass(object):
    def __init__(self, attribute='default', name='Instance'):
        self.name = name  # public attribute
        self.__attribute = attribute  # 'private' attribute

    def __str__(self):
        return '{} has attribute {}'.format(self.name, self.__attribute)

More on classes

classes, types, introspection

mapping operators to special methods

class MyClass(object):
    def __init__(self, param1 = 0):
        print('in constructor')
        self.value = param1 # set a local instant variable named value 

    def __str__(self):
        print('in str')
        return 'Val is : {}'.format(str(self.value))

    def __add__(self, param2):
        print('in add')
        result = self.value + param2.value
        return MyClass(result)

Inheritance

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容