python - class

作者: Pingouin | 来源:发表于2021-01-27 00:37 被阅读0次

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

相关文章

  • Python - Extracting ZIP

    Zip format and Python ZipFile class class zipfile.ZipFile...

  • Fluent系列2

    First-Class Functions Functions in Python are first-class...

  • 2018-12-03 类class

    Python入门之类(class)

  • Python Note4 (OOP)

    Labels: Python, Class,Object Ref:Python Classes/Objects h...

  • class python

    If we wanted to extract a header, grab all the columns, a...

  • Python -- Class

    创建 Class 实例化 Class 传递 实例化后的 Class, 比如 bmw ,叫做对象;传递对象(及其他可...

  • python - class

    类的编码风格 1、类名应采用驼峰命名法 , 即将类名中的每个单词的首字母都大写, 而不使用下划线。 实例名和模块名...

  • Python class

    定义一个类 创建一个对象 Python会为你完成对象创建,然后你可以使用init()方法定制对象的初始化。 每个方...

  • python class

    instance variables and class variables @classmethod means...

  • 【Python】Class

    面对对象编程的三大特性:封装,继承,多态。 封装:create class class封装了Attribute和m...

网友评论

    本文标题:python - class

    本文链接:https://www.haomeiwen.com/subject/dndwzktx.html