Use dot notation to call a function without using parentheses

2020-12-22 Thread Walk More
I am trying to use dot notation to call a function without using parentheses, 
see code section with ***
I have looked into SimpleNamespace, namedTuple, dataclass... but no luck.
Below is my sample code to date.
Any suggestions?



class MyTest:
def __init__(self):
self.page1 = Page()
self.page1.top = Counts()
#self.page1.middle = Layout()
#self.page1.bottom = Layout()

# access of variables created on the fly using dot notation work.
self.page1.top.item1 = 5
self.page1.top.name1 = "Harry"
print ("Variables created on the fly: ", self.page1.top.item1, 
self.page1.top.name1)


# access of a predefined class variable using dot notation work.
print ("Start of predefined class variable access: ", 
self.page1.top.totalCount)
self.page1.top.totalCount = 22
self.page1.top.totalCount = self.page1.top.totalCount + 3
print ("End of predefined class variable access: ", 
self.page1.top.totalCount)


# function calls using parentheses using dot notation work.
print ("Start of function calls: ", self.page1.top.getRunningSum())
self.page1.top.addRunningSum(5)
self.page1.top.addRunningSum(200)
endValue = self.page1.top.getRunningSum()
print ("End of function calls: ", endValue) 


# *** This is the syntax I would like to use. ***
# function calls not using parentheses DO NOT WORK using dot notation.
self.page1.top.addRunningSum = 6
t = self.page1.top.getRunningSum
print (t)


class Page ():
def __init__ (self):
pass


class Counts():
def __init__ (self):
self.totalCount = 0

def addRunningSum(self, indata):
self.totalCount = self.totalCount + indata

def getRunningSum (self):
return self.totalCount


if __name__ == "__main__":
MyTest()

#end of program
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Use dot notation to call a function without using parentheses

2020-12-22 Thread Walk More
On Tuesday, December 22, 2020 at 6:31:08 AM UTC-5, Python wrote:
> Walk More wrote: 
> > I am trying to use dot notation to call a function without using 
> > parentheses, see code section with *** 
> > I have looked into SimpleNamespace, namedTuple, dataclass... but no luck. 
> > Below is my sample code to date. 
> > Any suggestions?
> accessors. 
> 
> class myClass: 
> @property 
> def data(self): 
> print('read data attribute') 
> return self._data 
> @data.setter 
> def data(self, value): 
> print('write data attribute') 
> self._data = value 
> def __init__(self, data=None): 
> self.data = data 
> 
> o = myClass('spam') 
> 
> print(o.data) 
> 
> o.data = 'ham' 
> 
> output: 
> 
> write data attribute 
> read data attribute 
> spam 
> write data attribute


Python,
Thank you very much for your solution.



-- 
https://mail.python.org/mailman/listinfo/python-list