20190819 Python 练习记录

class Student(object):
    def __init__(self,name,score):
        self.__name=name
        self.__score=score

    def print_score(self):
        print('%s %s'%(self.__name,self.__score))
bart=Student('bob',90)
bart.print_score()

class Student(object):
    def __init__(self,name,score):
        self.name = name
        self.score=score
bart=Student('jb',78)
bart.name
print('%s'%bart.name)
class Student(object):
    def __init__(self,name,score):
        self.__name = name
        self.__score=score
    def print_score(self):
        print("%s %s" %(self.__name,self.__score))
bart=Student('jb',78)
bart.print_score()

print('%s' % bart._Student__name)#可以用着这种方式访问私有变量


>>> type('456')
<class 'str'>
>>> type(None)
<class 'NoneType'>
>>> import types
>>> def fn():
    pass

>>> type(fn)==types.FuncionType
Traceback (most recent call last):
  File "<pyshell>", line 1, in <module>
AttributeError: module 'types' has no attribute 'FuncionType'
>>> type(fn)==types.FunctionType
True
>>> tpye(abs)==types.BuiltinFunctionType
Traceback (most recent call last):
  File "<pyshell>", line 1, in <module>
NameError: name 'tpye' is not defined
>>> type(abs)==types.BuiltinFunctionType
True
>>> dir('123')
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
>>> len('ABC')
3
>>> 'ACBD'.__len__()
4
>>> 'ACBD'.__len__
<method-wrapper '__len__' of str object at 0x034E0860>
>>> 'ACBD'.__len__()
4
>>> class MyObject(object):
    def __init__(self):
        self.x=9
    def power(self):
        return self.x*self.x
    
>>> obj=MyObject()
>>> hasattr(obj,'x')
True
>>> hasattr(obj,'y')
False
>>> setattr(obj,'y',18)
>>> hasattr(obj,'y')
True
>>> getattr(obj,'y')
18
>>> getattr(obj,'z',404)
404
>>> getattr(obj,'z')
Traceback (most recent call last):
  File "<pyshell>", line 1, in <module>
AttributeError: 'MyObject' object has no attribute 'z'
>>> getattr(obj,'z','this is a error')
'this is a error'


>>> class Student(object):
    pass

>>> s=student()
Traceback (most recent call last):
  File "<pyshell>", line 1, in <module>
NameError: name 'student' is not defined
>>> s=Student()
>>> s.name = 'Mickh'
>>> print(s.name)
Mickh
>>> print(Student.name)
Traceback (most recent call last):
  File "<pyshell>", line 1, in <module>
AttributeError: type object 'Student' has no attribute 'name'
>>> def set_age(self,age):
    self.age=age
    
>>> from types import MethodType
>>> s.set_age=MethodType(set_age,s)
>>> s.set_age(25)
>>> s.age
25
>>> class Student(object):
    __slots__=('name','age')
    
>>> s=Student()
>>> s.name ='xd'
>>> s.age =34
>>> s.score=65
Traceback (most recent call last):
  File "<pyshell>", line 1, in <module>
AttributeError: 'Student' object has no attribute 'score'
>>> 

class Student(object):
    def get_score(self):
        return self._score
    def set_score(self,value):
        if not isinstance(value,int):
            raise ValueError('score must be an integer!')
        if value <0 or value >100:
            raise ValueError('score must between 0~100')
        self._score=value  

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值