Classes and instances
Python is an object-oriented language, and the most important object-oriented concepts are classes and instances, I remember when I first learned these concepts do not quite understand, until the teacher said "things are grouped together". That's right, classes, categorization.
Birds of a feather flock together.
Classes are actually groups of things that share the same characteristics, such as people.
class Person(object): pass
We've defined the class human, but human has some characteristics, like two eyes and a mouth, and we're going to add those in.
class Person(object): eyes = 2 mouth = 1
I've already put in some information about the person, but the person has a name, like me, Mink. Well, I can't be mean to myself. I've got to add that.
class Person(object): eyes = 2 mouth = 1 name = mink
It's perfect. A man has finally done it. It took God a day and me a minute. Let's read the message. Man has two eyes, one mouth, and his name is Mink. That's not right. Mink is my name. Why is a human named Mink?
Mink is a human name, human name is mink is obviously wrong, "wo" should be human individual, is a single example
class Person(object): eyes = 2 mouth = 1 def __init__(self, name): = name me = Person('mink')
Now that I finally have my own name instead of sharing it with everyone, this method is called instancing but I have a skill that no one else does, I'm unaffected by gravity.
class Person(object): eyes = 2 mouth = 1 def __init__(self, name) = name def jineng(self, txt): print "%s %s" % (, txt) me = Person('mink') ("I'm not affected by gravity. I can fly.")
Class methods and static methods
You can often see @classmethod and @staticmethod in python, referred to as class methods and instance methods.
class Animal(object): name = 'lili' age = 1 cat = Animal() print , # print 'lili' 1
Creates an Animal class, generates an instance of cat, prints the name and age of the cat, you can see that the attributes returned are properties of the Animal class, i.e., the instance accesses the class'scausality
# The display is the same print , print , do sth (for sb)Animalclass to add a method(function (math.)) class Animal(object): name = 'lili' age = 1 def edit(self, name, age): = name = age cat = Animal() ('rol', 2) print , # print 'rol' 2 print , # print 'lili' 1
This means that the method added by default is an instance method, which modifies the properties of the instance without changing the properties of the class.
# Let's modify this function def edit(self, name, age): name = name = age cat = Animal() ('rol', 2) print , # pirnt 'rol' 2 print , # print 'lili' 1
What if I want to modify the properties of a class even though the instance method cannot do so?
# Modify edit again @classmethod def edit(cls, name, age): = name = age cat = Animal() ('rol', 2) print , # print 'rol' 2 print , # print 'rol' 2
It should be noted that the first parameter of the edit function has self changed to cls, python suggests that you use cls in class methods and self in instance methods, and it shows that instances can use class methods (functions).
Then I'm adding the init method to this class to initialize the properties
class Animal(object): name = 'lili' age = 1 def __init__(self, name, age): = name = age ... cat = Animal('kuku', 4) ('rol', 2) print , # print 'kuku' 4 print , # print 'rol' 2
With the addition of __init__, cat no longer uses the properties of the class, and modifying the edit method does not change the properties of the cat instance.
# Add staticmethod @staticmethod def say_name(name=None): if not name: name = print 'my name is %s.' % name cat = Animal('kaka', 3) cat.say_name() # If you run it, it will report NameError: global name 'self' is not defined # Is it possible that he didn't add a self field, so he didn't find it # def say_name(self, name=None): ... cat.say_name() # TypeError: say_name() takes at least 1 argument(0 given), Show missing parameters
This means that staticmethod can't use the properties and methods of an instance, and it certainly can't use a class. What about the other way around?
# Let's change the code # First create an instance of the method, he uses the class staticmethod @staticmethod def say_name(name): print 'my name is %s.' % name def say(self): self.say_name() @classmethod def _say(cls): cls.say_name() cat = Animal('kaka', 3) () cat._say()
The staticmethod can be accessed through class methods and instance methods.
To summarize.
Static method (staticmethod)
- Static methods cannot use the properties and methods of an instance
- Static methods cannot use class properties and methods
- Static methods can be called by class or instance
- Static methods are equivalent to global functions scoped in the class
Class Methods
- Class methods can use class properties and methods
- Class methods can use static methods
- Methods of a class can be called from the class or instance