标签:
Modile:
1. Python module have to be saved as .py file.
Classes:
1. In the class, we have to define a function __init__(self) first. And __init__ is a method whenever we create an instance of a class. The __init__ takes a self parameter which automatically being passed into the class.
2. All the propertys in the class need to be after self.
3. When you create an instance for a class, it is better to assign the class name into a variable.
4. We define the behavior of an instance into a methods.
5. Example 1:
class Team():#Define a class named Team
def __init__(self,name):# Call the __init__ method which has two parameters self and name. Parameter self is a default parameter, and name is customized parameter
self.name = name # The name in self.name is not the parameter name. And it is treated as a property of the class with prefix self. . The ‘name‘ after equal notation is the parameter ‘name‘
giants = Team("New York Giants")# when we call the class, we can ingore the defalut paramter self.Whenever we call a method on an instance,we never explicitly specify self in htemethod cal.
print (giants.name)
6. Example 2:
class Team():
def __init__(self,name):
self.name = name
def print_name(self):# Here, the print_name is called instance method. Unlike __init__, instance method can not being automatically called. But it also has the default parameter self
print(self.name)
bucs.print_name() # In order to call the instance method, we have to call it explicitly in the code by using dot notation.
7. self argument in the method dose not mean the method itself, it means the class itself.
8. There are two ways to set the arguments. One is setting the parameters for the method. The other one is to set self.xxx = xxx in the __init__() method. The former can only being used in this method in the class, the latter can be used in all the classes.
标签:
原文地址:http://www.cnblogs.com/kingoscar/p/5928703.html