py 类,类成员(变量、方法)、继承、关键方法、with、callback、exec、eval、字符转型等,测试

py 类,类成员(变量、方法)、继承、关键方法、with、callback、exec、eval、字符转型等,测试;

在以前的公司,发现,服务端同事都使用c/c++写底层,python来写业务逻辑,说是非常方便,快速;因为语法很精简单;

今天,为以后多平台下写统一的工具处理;

所以开始了py 的从零开始的学习


class Person:
	name = "";
	personVar1 = "";
	def __init__(self, name = "name"):
		print "__init__(self, name)";
		self.name = name;
	
	def printSelf(self):
		print str(self);
	
	def setPersonVar1(self, value):
		self.personVar1=value;
	
	def getPersonVar1(self):
		return self.personVar1;
		
	def showMeYourName(self):
		print "My name is :" + self.name;

class Jave(Person):

	_privateVar1 = "this is privateVar1";
	
	def __init__(self):
		Person.__init__(self, "Jave"); # call the super class.__init__ method
		self._memMap = {};
	
#	def showMeYourName(self): # override super class.showMeYourName(self) method
#		print self.name + " is my name";
		
	def showMeYourName(self, str1="test"):
		var1 = "test1";
		var2 = 123;
		print locals();
		print "This is showMeYourName(self, str1)" + str1;
		
	def __str__(self):
		return "This is Class Jave.__str__(self) method!"
		
	def setCallback(self, callback):
		# self.callback = callback;
		self.__setitem__("callback", callback);
	
	def __setitem__(self, key, value):
		self._memMap[key] = value;
		
	def __getitem__(self, key):
		if(self._memMap.has_key(key)):
			return self._memMap[key];
		else:
			return 0;
	
	def actionCallback(self):
		if(self["callback"]):
			self["callback"]();
		else:
			print "no attribute 'callback', so can not call it!";
	
	def _privateMethod(self):
		print "this is private method";
		
	def __del__(self):
		print "__del__ method was action!"
	
	def __call__(self, *args):
		print "__call__(self, *args) method was action!" + str(*args);

print "cmp(2,3)==" + str(cmp(2,3));
print "len(range(10))==" + str(len(range(10)));
# print "hash({name:\"test\"}==" + str(hash({"name":"test"}));
p = Person();
print "new Person()==" + str(hash(p));
p.name = "test";
print "p.name = " + p.name;
print "new Person()==" + str(hash(p));
p.printSelf();
p.setPersonVar1("PersonVar1IsString");
print "get PersonVar1, value == "+p.getPersonVar1();
jave = Jave();

def theCallback():
	print "this is callback method";

def __exit__():
	print "__exit__ was action!";

class WithObj:
	
	def __enter__(self):
		print "__enter__() was action!";
	
	def __context__(self):
		print "__context__() was action!";
		return self;
		
	def __exit__(self, *args):
		print "__exit__() was action!";
	
	def __str__(self):
		print "WithObj.__str__ was action!"
	
with WithObj() as w:
	print w;
print w;
#jave.setCallback(theCallback);
jave.actionCallback();

jave._privateMethod();

print jave._privateVar1;

jave.__setitem__("test", "123test");	
print jave.__getitem__("test");
print jave;
print jave.showMeYourName();
print jave.showMeYourName("args");

jave();
jave("again!!!!!");

# del jave;
exec "del jave";
# eval("exp");
print eval("100+1");

print "2.0 / 3 == " + str(2.0 / 3);
print "2.0 // 3 == " + str(2.0 // 3);

print 0xffffff;
print hex(0xffffff);
print hex(int("0xff", 16));
print float("3.1415926");


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值