node2:/root/20200605#cat t2.py
class a100(object):
def encode(self,a):
return a
x=a100()
print x
print type(x)
print x.encode('dddddd')
You have mail in /var/spool/mail/root
node2:/root/20200605#python t2.py
<__main__.a100 object at 0x7f532e0048d0>
<class '__main__.a100'>
dddddd
当创建实例时,__init__()方法会被自动调用
实例方法:在类中定义的函数,第一个参数是self,指向调用该方法的实例本身。
node2:/root/20200605#cat t4.py
class Person(object):
def __init__(self, name):
self.__name = name
def get_name(self):
return self.__name
a=Person('xyz')
print a
print type(a)
print a.get_name()
node2:/root/20200605#python t4.py
<__main__.Person object at 0x7f6659b9c690>
<class '__main__.Person'>
xyz