mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-11-23 22:27:35 +03:00
Done with Chinese, mostly
This commit is contained in:
parent
191a0c7050
commit
cd4c3a4ad8
@ -123,8 +123,7 @@ False or True #=> True
|
|||||||
# None是一个对象
|
# None是一个对象
|
||||||
None # => None
|
None # => None
|
||||||
|
|
||||||
# Don't use the equality "==" symbol to compare objects to None
|
# 当与None进行比较时不要用 ==,要用is。is是用来比较两个变量是否指向同一个对象。
|
||||||
# Use "is" instead. This checks for equality of object identity.
|
|
||||||
"etc" is None # => False
|
"etc" is None # => False
|
||||||
None is None # => True
|
None is None # => True
|
||||||
|
|
||||||
@ -482,52 +481,50 @@ filter(lambda x: x > 5, [3, 4, 5, 6, 7]) # => [6, 7]
|
|||||||
####################################################
|
####################################################
|
||||||
|
|
||||||
|
|
||||||
# We subclass from object to get a class.
|
# 定义一个继承object的类
|
||||||
class Human(object):
|
class Human(object):
|
||||||
|
|
||||||
# A class attribute. It is shared by all instances of this class
|
# 类属性,被所有此类的实例共用。
|
||||||
species = "H. sapiens"
|
species = "H. sapiens"
|
||||||
|
|
||||||
# Basic initializer, this is called when this class is instantiated.
|
# 构造方法,当实例被初始化时被调用。注意名字前后的双下划线,这是表明这个属
|
||||||
# Note that the double leading and trailing underscores denote objects
|
# 性或方法对Python有特殊意义,但是允许用户自行定义。你自己取名时不应该用这
|
||||||
# or attributes that are used by python but that live in user-controlled
|
# 种格式。
|
||||||
# namespaces. You should not invent such names on your own.
|
|
||||||
def __init__(self, name):
|
def __init__(self, name):
|
||||||
# Assign the argument to the instance's name attribute
|
# Assign the argument to the instance's name attribute
|
||||||
self.name = name
|
self.name = name
|
||||||
|
|
||||||
# An instance method. All methods take "self" as the first argument
|
# 实例方法,第一个参数总是self,就是这个实例对象
|
||||||
def say(self, msg):
|
def say(self, msg):
|
||||||
return "{name}: {message}".format(name=self.name, message=msg)
|
return "{name}: {message}".format(name=self.name, message=msg)
|
||||||
|
|
||||||
# A class method is shared among all instances
|
# 类方法,被所有此类的实例共用。第一个参数是这个类对象。
|
||||||
# They are called with the calling class as the first argument
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_species(cls):
|
def get_species(cls):
|
||||||
return cls.species
|
return cls.species
|
||||||
|
|
||||||
# A static method is called without a class or instance reference
|
# 静态方法。调用时没有实例或类的绑定。
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def grunt():
|
def grunt():
|
||||||
return "*grunt*"
|
return "*grunt*"
|
||||||
|
|
||||||
|
|
||||||
# Instantiate a class
|
# 构造一个实例
|
||||||
i = Human(name="Ian")
|
i = Human(name="Ian")
|
||||||
print(i.say("hi")) # prints out "Ian: hi"
|
print(i.say("hi")) # 印出 "Ian: hi"
|
||||||
|
|
||||||
j = Human("Joel")
|
j = Human("Joel")
|
||||||
print(j.say("hello")) # prints out "Joel: hello"
|
print(j.say("hello")) # 印出 "Joel: hello"
|
||||||
|
|
||||||
# Call our class method
|
# 调用一个类方法
|
||||||
i.get_species() # => "H. sapiens"
|
i.get_species() # => "H. sapiens"
|
||||||
|
|
||||||
# Change the shared attribute
|
# 改一个共用的类属性
|
||||||
Human.species = "H. neanderthalensis"
|
Human.species = "H. neanderthalensis"
|
||||||
i.get_species() # => "H. neanderthalensis"
|
i.get_species() # => "H. neanderthalensis"
|
||||||
j.get_species() # => "H. neanderthalensis"
|
j.get_species() # => "H. neanderthalensis"
|
||||||
|
|
||||||
# Call the static method
|
# 调用静态方法
|
||||||
Human.grunt() # => "*grunt*"
|
Human.grunt() # => "*grunt*"
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user