Python bit by bit accumulation

Python class

Define a class

Method * * init()**
Python automatically runs this method whenever a new instance is created. In this method name, there are two underscores at the beginning and the end, which is a convention to avoid name conflicts between Python default methods and ordinary methods.

class Dog():
    """Dog like class"""
    def __init__(self,name,age):
        """Initialize properties"""
        self.name=name
        self.age=age
    def sit(self):
        """Dog squatting"""
        print(self.name.title()+" is now sitting.")
    def roll(self):
        """Dog rolling"""
        print(self.name.title()+" is now rolling")

Calls to properties and methods

dog=Dog("bobby",4)
print("The dog's name is "+dog.name.title()+",age is "+str(dog.age)+".")
dog.sit()
dog.roll()

Program output:

The dog's name is Bobby,age is 4.
Bobby is now sitting.
Bobbyis now rolling

Modify the value of the property

There are three different ways to modify the value of an attribute: modifying directly through an instance, setting by a method, and increasing by a method.
1. Modify directly by instance

class Animal():
    def __init__(self,name,age):
        self.name=name
        self.age=age
        self.home='china'
    def get_info(self):
        info=self.name+" "+str(self.age)+" "+self.home
        return info
    
bird=Animal('lucy',3)
print(bird.get_info())
bird.age=bird.age+1
print(bird.get_info())

Program output:

lucy 3 china
lucy 4 china

2. Modify attribute value by method

class Animal():
    def __init__(self,name,age):
        self.name=name
        self.age=age
        self.home='china'
    def set_name(self,name):
        self.name=name
    def get_info(self):
        info=self.name+" "+str(self.age)+" "+self.home
        return info
    
bird=Animal('lucy',3)
print(bird.get_info())
bird.set_name('qiu')
bird.set_age(6)
print(bird.get_info())

Program output:

lucy 3 china
qiu 6 china

3. Increase the value of attribute by method

class Animal():
    def __init__(self,name,age):
        self.name=name
        self.age=age
        self.home='china'
    def update_age(self):
        self.age+=1
    def get_info(self):
        info=self.name+" "+str(self.age)+" "+self.home
        return info
bird=Animal('lucy',3)
print(bird.get_info())
bird.update_age()
bird.update_age()
print(bird.get_info())

Program output:

lucy 3 china
lucy 5 china

inherit

When a class inherits another class, it will automatically obtain all the properties and methods of the other class; the original class is called the parent class, and the new class is called the child class. The child class inherits all the properties and methods of the parent class, and can also define its own properties and methods.
When you create a subclass, the subclass must be included in the current file and preceded by the subclass.
When defining a subclass, you must specify the name of the parent class in parentheses.

class Animal():
    """Create an animal class"""
    def __init__(self,name,age):
        self.name=name
        self.age=age
    def set_name(self,name):
        self.name=name
    def set_age(self,age):
        self.age=age
    def eat(self):
        print(self.name.title()+" can eat")
    def sleep(self):
        print(self.name.title()+"can sleep")
    def get_info(self):
        info=self.name+" "+str(self.age)
        return info
    
class Cat(Animal):
    """establish Cat inherit Animal"""
    def __init__(self,name,age):
        super().__init__(name,age)
        self.home='China'
    def get_info(self):
         info=self.name+" "+str(self.age)+" "+self.home
         return info
    
cat=Cat("kitty",2)
print(cat.get_info())
cat.eat()
cat.sleep()

Program output:

kitty 2 China
Kitty can eat
Kittycan sleep

Start by making the code structure as simple as possible. First, complete all the work in one file as much as possible, make sure everything can work normally, and then move the class to a separate module.

Class coding style

1. The hump naming method shall be adopted for the class name, that is, each word in the class name shall be capitalized without underline.

2. Instance name and module name are in lowercase format and underlined between words.

3. For each class, include a document string immediately after the class definition. Each module should also contain a document string that describes what classes can be used for.

4. You can use blank lines to organize your code, but don't abuse them. In a class, you can use an empty row to separate methods; in a module, you can use two empty rows to separate classes.

5. If you need to import the modules in the standard library and the functions you write at the same time, first write the import statement of the standard library module, then add a blank line, and then write the import statement of the module you write yourself.

Tags: Python Attribute

Posted on Fri, 19 Jun 2020 04:09:39 -0400 by vertmonkee