def getchar(echo): if echo: B = msvcrt.getwche else: B = msvcrt.getwch A = B() if A in ('\x00', 'à'): A += B() _translate_ch_to_exc(A) return A
def button(groupid): #d=groupid #return jsonify(d) #B=Mix_group if groupid == '000': return str(B.A()) #return str(B.catch_powder()) elif groupid == '100': return str(B.B()) #return str(B.mixed_powder()) elif groupid == '200': return str(B.C()) #return str(B.powder_feeding()) elif groupid == '300': return str(B.D()) #return str(B.mixed_abrasive()) elif groupid == '400': return str(B.E()) #return str(B.make_wetting_agent()) elif groupid == '500': return str(B.F()) #return str(B.add_wetting_agent()) elif groupid == '600': return str(B.G())
#!usr/bin/env python # classSuperclassExample.py >>> class A: def hello(self): print("Hello, I am A.") >>> class B(A): pass >>> a = A() >>> b = B() >>> >>> a.hello() Hello, I am A. >>> b.hello() # b/c B does not define a hello method of its own Hello, I am A. # the original A message is printed in class B. >>> # Now lets add basic functionality in the subclass (ie class B). So lets add methods to the subclass. # Here B own method will override the superclass's method >>> class B(A): # rebuild class B def hello(self): # add new method print("Hello, from B.") # add new attribute >>> >>> b.hello() # call original attribute b.hello (which pulls from class A) Hello, I am A. >>> b = B() # now rebind b to new class B >>> b.hello()
#파이썬에서는 import라고 하면 뒤에 불러올 파이썬 파일을 찾음!! import a import B #import a, B라고 하는 걸 더 권장 print(a.a()) print(B.B()) #yunjin이라는 모듈에서 a 함수만 불러오고 싶을 땐? from yunjin import a print(a()) #이렇게 가져오면 yunjin을 명시할 필요X #아예 모듈을 가져오면 명시해야 하지만... from yunjin import B as print_B #B를 print_B라는 이름으로 가져오고 싶다. print(print_B()) import a as print_a # 파일 이름인 모듈 a를 print_a라는 함수 이름으로 쓰고 싶다 print(print_a.a()) #from 없이 import만 있으면 모듈을 가져오는 것이고, from과 함께 있으면 함수만 가져오는 것! #import가 가져오는 건 모듈일 수도, 함수일 수도 있다!
#!/usr/bin/python from A import * from B import * from C import * data = [1, 2, 3, 4, 5] b = B(data, "B") c = C(data, "C") b.printme() b.tu1() b.printme() c.printme() c.tu1() b.printme() c.printme()
... def __fa(self): ... print('from B') ... >>> b=B() >>> b.test() from A #在子类定义的__x不会覆盖在父类定义的__x,因为子类中变形成了:_子类名__x,而父类中变形成了:_父类名__x,即双下滑线开头的属性在继承给子类时,子类是无法覆盖的。 class Foo: def __f1(self): #_Foo__f1 print('Foo.f1') def f2(self): self.__f1() #self._Foo_f1 class Bar(Foo): def __f1(self): #_Bar__f1 print('Bar.f1') # b=Bar() # b.f2() \封装不是单纯意义的隐藏 封装的真谛在于明确地区分内外,封装的属性可以直接在内部使用,而不能被外部直接使用,然而定义属性的目的终归是要用,外部要想用类隐藏的属性,需要我们为其开辟接口, 让外部能够间接地用到我们隐藏起来的属性,那这么做的意义何在??? # 1:封装数据:将数据隐藏起来这不是目的。隐藏起来然后对外提供操作该数据的接口,然后我们可以在接口附加上对该数据操作的限制,以此完成对数据属性操作的严格控制。 class Teacher: def __init__(self,name,age): # self.__name=name # self.__age=age self.set_info(name,age) def tell_info(self): print('姓名:%s,年龄:%s' %(self.__name,self.__age)) def set_info(self,name,age): if not isinstance(name,str): raise TypeError('姓名必须是字符串类型') if not isinstance(age,int): raise TypeError('年龄必须是整型') self.__name=name self.__age=age t=Teacher('egon',18) t.tell_info() t.set_info('egon',19) t.tell_info() # 2:封装函数属性:为了隔离复杂度 封装方法举例: 1. 你的身体没有一处不体现着封装的概念:你的身体把膀胱尿道等等这些尿的功能隐藏了起来,然后为你提供一个尿的接口就可以了(接口就是你的。。。,),你总不能把膀胱挂在身体外面,上厕所的时候就跟别人炫耀:hi,man,你瞅我的膀胱,看看我是怎么尿的。 2. 电视机本身是一个黑盒子,隐藏了所有细节,但是一定会对外提供了一堆按钮,这些按钮也正是接口的概念,所以说,封装并不是单纯意义的隐藏!!! 3. 快门就是傻瓜相机为傻瓜们提供的方法,该方法将内部复杂的照相功能都隐藏起来了 提示:在编程语言里,对外提供的接口(接口可理解为了一个入口),可以是函数,称为接口函数,这与接口的概念还不一样,接口代表一组接口函数的集合体。 #取款是功能,而这个功能有很多功能组成:插卡、密码认证、输入金额、打印账单、取钱 #对使用者来说,只需要知道取款这个功能即可,其余功能我们都可以隐藏起来,很明显这么做 #隔离了复杂度,同时也提升了安全性 class ATM: def __card(self): print('插卡') def __auth(self): print('用户认证') def __input(self): print('输入取款金额') def __print_bill(self): print('打印账单') def __take_money(self): print('取款') def withdraw(self): self.__card() self.__auth() self.__input() self.__print_bill() self.__take_money() a=ATM() a.withdraw() \封装与扩展性 封装在于明确区分内外,使得类实现者可以修改封装内的东西而不影响外部调用者的代码;而外部使用用者只知道一个接口(函数),只要接口(函数)名、参数不变, 使用者的代码永远无需改变。这就提供一个良好的合作基础——或者说,只要接口这个基础约定不变,则代码改变不足为虑。 #类的设计者 class Room: def __init__(self,name,owner,width,length,high): self.name=name self.owner=owner self.__width=width self.__length=length self.__high=high def tell_area(self): # 对外提供的接口,隐藏了内部的实现细节,此时我们想求的是面积 return self.__width * self.__length #使用者 >>> r1=Room('卧室','egon',20,20,20) >>> r1.tell_area() # 使用者调用接口tell_area #类的设计者,轻松的扩展了功能,而类的使用者完全不需要改变自己的代码 class Room: def __init__(self,name,owner,width,length,high): self.name=name self.owner=owner self.__width=width self.__length=length self.__high=high def tell_area(self): # 对外提供的接口,隐藏内部实现,此时我们想求的是体积,内部逻辑变了,只需求修该下列一行就可以很简答的实现,而且外部调用感知不到,仍然使用该方法,但是功能已经变了 return self.__width * self.__length * self.__high #对于仍然在使用tell_area接口的人来说,根本无需改动自己的代码,就可以用上新功能 >>> r1.tell_area()
To override a method in the base class, sub class needs to define a method of same signature. (i.e same method name and same number of parameters as method in base class). class A(): def __init__(self): self.__x = 1 def m1(self): print("m1 from A") class B(A): def __init__(self): self.__y = 1 def m1(self): print("m1 from B") c = B() c.m1() 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 class A(): def __init__(self): self.__x = 1 def m1(self): print("m1 from A") class B(A): def __init__(self): self.__y = 1 def m1(self): print("m1 from B") c = B() c.m1() Expected Output: m1 from B 1 m1 from B Here we are overriding m1() method from the base class. Try commenting m1() method in B class and now m1() method from Base class i.e class A will run. Expected Output: m1 from A 1 m1 from A isinstance() function isinstance() function is used to determine whether the object is an instance of the class or not. Syntax: isinstance(object, class_type) >>> isinstance(1, int) True >>> isinstance(1.2, int) False >>> isinstance([1,2,3,4], list) True 1 2 3 4 5 6 7 8 >>> isinstance(1, int) True >>> isinstance(1.2, int) False >>> isinstance([1,2,3,4], list) True