Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Inferring background code.
#1
Given an object instantiation and invocation, can I broadly say the following?

Given the following two lines of code,
test = Test()
test()
Broadly, it could originate from one of the following two situations,

Situation 1:
class Test():  
   def __init__(self): # constructor  
      pass  
  
   def __call__(self): # called during object() invocation  
      pass
Situation 2:
def Test():
	def innerTest():
		pass

	return innerTest
Am I right? Or are there other broad categories I am missing.

I come from Java background, and you can tell a lot by variable declaration and innovation, which does not seem to be the case for Python.
Reply
#2
Parentheses can also imply tuples or sets. So, wondering bout the question behind the question - are you trying to decompile something, understand some given code, or just asking out of interest?
Reply
#3
(Apr-17-2023, 11:36 AM)jefsummers Wrote: Parentheses can also imply tuples or sets. So, wondering bout the question behind the question - are you trying to decompile something, understand some given code, or just asking out of interest?

But () can not precede an identifier, such as <identifier>(), unless it is one of the two cases I've mentioned.

I am asking out of interest. I come from a strongly and statically typed language - where you can tell a lot about the program structure during compile time. But I have found that not to be the case for Python. But I am still trying to figure out how much I can infer program structure only by reading the code.
Reply
#4
With the two lines of code, one can only deduce that Test is a callable object which invocation returns another callable object. Now you only need to find all the ways to create a callable object in Python, for example
class Spam:
    def eggs(self):
        return self.eggs

Test = Spam().eggs
test = Test()
test()
quazirfan likes this post
Reply
#5
(Apr-18-2023, 09:42 AM)Gribouillis Wrote: With the two lines of code, one can only deduce that Test is a callable object which invocation returns another callable object.

Double checking: So the only thing we can deduce from these two lines is that callable() will return true for both Test() and test(), and nothing else.
Reply
#6
(Apr-19-2023, 05:26 AM)quazirfan Wrote: we can deduce from these two lines is that callable() will return true
It is highly probable, but it is not certain because you don't know the intention of the person who wrote this code. For example it could be
>>> def Test():
...     return 'spam'
... 
>>> def func():
...     test = Test()
...     test()
... 
>>> try:
...     func()
... except TypeError:
...     print('Ok')
... 
Ok
Here Test() returns a non callable. Calling it will raise TypeError, but perhaps this is the side effect that the programmer was trying to create.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020