Python的访问限制

1、python中初始化实例属性

classmethod, staticmethod, self

_arr, _internal. __private_method,__private_attr

1

必须在__init__(self,…)方法内(注意:双下划线)初始化实例,第一个参数必须为self

1

如需动态添加属性,可用 **kw

1

2

3

4

5

6

7

8

9

10

class Person(object):

    def __init__(self,name,gender,birth,**kw):

        self.name = name

        self.gender = gender

        self.birth = birth

        for k,v in kw.iteritems() :

            setattr(self,k,v)

xiaoming = Person('Xiao Ming', 'Male', '1990-1-1', job='Student')

print xiaoming.name

print xiaoming.job

 

2、python中访问限制

Python对属性权限的控制是通过属性名来实现的,如果一个属性由双下划线开头(__),该属性就无法被外部访问。

如果一个属性以"__xxx__"的形式定义,那它又可以被外部访问了,以"__xxx__"定义的属性在Python的类中被称为特殊属性,有很多预定义的特殊属性可以使用,通常我们不要把普通属性用"__xxx__"定义。

以单下划线开头的属性"_xxx"虽然也可以被外部访问,但是,按照习惯,他们不应该被外部访问。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

class Person(object):

    def __init__(self, name):

        self.name = name

        self._title = 'Mr'

        self.__job = 'Student'

p = Person('Bob')

print p.name

# => Bob

print p._title

# => Mr

print p.__job

# => Error

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

AttributeError: 'Person' object has no attribute '__job'

3、python中创建类属性

实例属性每个实例各自拥有,互相独立,而类属性有且只有一份。

1

2

3

4

class Person(object):

    address = 'Earth'

    def __init__(self, name):

        self.name = name

调用 Person.address 即可获得类属性,通过实例对象也可以获得类属性

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

class Person(object):

    count = 0

    def __init__(self,name='Smith'):

        Person.count = Person.count + 1

        self.name = name

 

p1 = Person('Bob')

print Person.count

 

p2 = Person('Alice')

print Person.count

 

p3 = Person('Tim')

print Person.count

 

p4 = Person()

1

2

p5 = Person()

print Person.count

1

注意,python不支持构造函数的重载。但通过设置默认值,调用构造函数时就可以省略参数。

1

如 p4 = Person()

4、python中类属性和实例属性名字冲突怎么办

1

在实例中修改类属性,可以吗?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

class Person(object):

    address = 'Earth'

    def __init__(self, name):

        self.name = name

 

p1 = Person('Bob')

p2 = Person('Alice')

 

print 'Person.address = ' + Person.address

 

p1.address = 'China'

print 'p1.address = ' + p1.address

 

print 'Person.address = ' + Person.address

print 'p2.address = ' + p2.address

1

当实例属性和类属性重名时,实例属性优先级高,它将屏蔽掉对类属性的访问。

1

<strong>类的属性也有访问权限,带双下划线(__xxx)的属性无法从外部访问。</strong>

1

<strong></strong> 

5、python中定义实例方法

类实例方法的定义在类体中,第一个参数必须是self,调用时无需显示传入。

1

2

3

4

5

6

7

8

9

class Person(object):

 

    def __init__(self, name):

        self.__name = name

 

    def get_name(self):

        return self.__name

a = Person("Jim")

a.get_name()

1

  

6、python中方法也是属性

1

上节中get_name使用下面方式调用时:

1

print p1.get_name

1

将返回一个方法,而不是函数返回值

1

fun = p1.get_name

1

print fun()

1

能得到name的值。

1

可以动态的把方法绑定到一个对象上去(虽然并不常用):

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

import types

def fn_get_grade(self):

    if self.score >= 80:

        return 'A'

    if self.score >= 60:

        return 'B'

    return 'C'

 

class Person(object):

    def __init__(self, name, score):

        self.name = name

        self.score = score

 

p1 = Person('Bob', 90)

p1.get_grade = types.MethodType(fn_get_grade, p1, Person)

print p1.get_grade()

# => A

p2 = Person('Alice', 65)

print p2.get_grade()

# ERROR: AttributeError: 'Person' object has no attribute 'get_grade'

# 因为p2实例并没有绑定get_grade

1

  

7、python中定义类方法

1

通过@classmethod语句将方法绑定到Person类上。

1

类方法中不用self,使用cls代指类。

1

2

3

4

5

6

7

8

9

10

11

12

13

class Person(object):

 

    __count = 0

 

    @classmethod

    def how_many(cls):

        return cls.__count

    def __init__(self,name):

        Person.__count += 1

        self.name = name

print Person.how_many()

p1 = Person('Bob')

print Person.how_many()

1

  

参考文献:http://www.imooc.com/code/6172

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值