0

I was just playing around, and due to a lack of creativity, I couldn't seem to come up with an original idea to code. So I thought of making a module called yep, that uses the name yep as much as possible. I am not sure why the below class yep can't be called in main -- I get an unboundLocal error. From what I see in my code, all the other yep's are in their scope, and only the class yep is global. Which means yep is defined. When I change the name of yep class and use that name in main, I don't get an undefined error.

import argparse

class yep:

    """
    """

    def __init__(self,yep:str) -> None:
        
        self.yep=yep
    
    def call_yep(self):

        """ 
        makes yep 
        Args:
            param1: yep instance
        Returns:
            yep    
        """
        yep=self.yep
        
        return yep

def main(args):

    yep=yep(args.yep).yep
    
    return yep
        
if __name__=='__main__':
    
    parser=argparse.ArgumentParser()
    parser.add_argument(
        '--yep',default='yep',help='its yep'
        )
    args=parser.parse_args()
    main(args)
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
pyeR_biz
  • 986
  • 12
  • 36
  • 2
    i.e. by seeing the line `yep=yep(args.yep).yep`, Python concludes that `yep` is a local variable to the function `main`. But then, upon trying to evaluate the RHS of the assignment that local variable still wasn't assigned a value... – Tomerikoo Jul 11 '21 at 15:01
  • It's so unusual I didn't know that, having been coding in Python for 3 years now. – pyeR_biz Jul 12 '21 at 08:49
  • 1
    Yep it's one of those small details one have to be familiar with which is not always the most intuitive. I can see why you would assume that when evaluating the RHS `yep` is still the class and after the assignment it will become the variable. I strongly recommend you to read the accepted answer in the link and ***all*** links in it, if you haven't done so already. Very helpful – Tomerikoo Jul 12 '21 at 09:22

0 Answers0