Skip to content
This repository was archived by the owner on Mar 29, 2021. It is now read-only.

Commit 24fa986

Browse files
author
JavaCode7
committed
add full stops and addMethod() function
1 parent eea6655 commit 24fa986

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

‎README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
print("A is for apple")
4444

4545
# add the attribute
46-
a.addAttr("printA", a_print)
46+
a.addMethod(a_print)
4747

4848
# refresh the object
4949
a_object = a.getObj()

‎demo.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@
2727
def applePrint():
2828
print("I love apples")
2929

30-
idealApple.addAttr("apples", applePrint)
30+
idealApple.addMethod(applePrint)
3131
appleObj = idealApple.getObj()
32-
appleObj.apples()
32+
appleObj.applePrint()

‎jsobj.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class JsObjectInstance(object):
44
class JsObject:
55
def __init__(self, template: dict):
66
self.template = template
7-
self.obj: Object = JsObjectInstance()
7+
self.obj: JsObjectInstance = JsObjectInstance()
88
i = 0
99
while i < len(self.template.keys()):
1010
setattr(self.obj, list(self.template.keys())[i], list(self.template.values())[i])
@@ -15,7 +15,14 @@ def addAttr(self, attr: str, value: any):
1515
self.template[attr] = value
1616
self.obj.__setattr__(attr, value)
1717
else:
18-
print("Attribute already taken, please use JsObject.setAttr()")
18+
print("Attribute already taken, please use JsObject.setAttr().")
19+
20+
def addMethod(self, method):
21+
if method.__name__ not in self.template.keys():
22+
self.template[method.__name__] = method
23+
self.obj.__setattr__(method.__name__, method)
24+
else:
25+
print("Method already present.")
1926

2027
def delAttr(self, attr: str):
2128
del self.template[attr]
@@ -26,7 +33,7 @@ def setAttr(self, attr: str, value: any):
2633
self.template[attr] = value
2734
setattr(self.obj, attr, value)
2835
else:
29-
print("Attribute not added, please use JsObject.addAttr()")
36+
print("Attribute not added, please use JsObject.addAttr().")
3037

3138
def getObj(self):
3239
return self.obj

0 commit comments

Comments
 (0)