Maxon Developers Maxon Developers
    • Documentation
      • Cinema 4D Python API
      • Cinema 4D C++ API
      • Cineware API
      • ZBrush Python API
      • ZBrush GoZ API
      • Code Examples on Github
    • Forum
    • Downloads
    • Support
      • Support Procedures
      • Registered Developer Program
      • Plugin IDs
      • Contact Us
    • Categories
      • Overview
      • News & Information
      • Cinema 4D SDK Support
      • Cineware SDK Support
      • ZBrush 4D SDK Support
      • Bugs
      • General Talk
    • Recent
    • Tags
    • Users
    • Login

    Confusing Behaviour (list=[] in function)

    Scheduled Pinned Locked Moved PYTHON Development
    5 Posts 0 Posters 384 Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • H Offline
      Helper
      last edited by

      On 12/09/2014 at 14:26, xxxxxxxx wrote:

      Hello everybody,

      I wrote the following lines and Im not able to understand what happens.

      import c4d  
      #Welcome to the world of Python  
        
      def A(lst=[]) :  
        lst.append("A")  
        return lst  
        
      def main() :  
        print A()
      

      If I execute the code first time the variable lst is printed in console exactly how I expect.

      >>>['A']
      

      If I execute second time...

      >>>['A']['A']
      

      third time...

      >>>['A']['A']['A']
      

      and so on.

      >>>['A']['A']['A']['A']  
      \>>>['A']['A']['A']['A']['A']  
      \>>>['A']['A']['A']['A']['A']['A']  
      \>>>['A']['A']['A']['A']['A']['A']['A']
      

      How can I stop that behaviour? 'del lst' didnt work.

      THX and Greeting
      rown

      1 Reply Last reply Reply Quote 0
      • H Offline
        Helper
        last edited by

        On 12/09/2014 at 17:08, xxxxxxxx wrote:

        This is because the argument to your function is only evaluated once when you first define the function. The default argument keeps getting used every time you call the function.

        One way to solve it is setting the default value of your argument to None and adjust the code block as necessary.

        1 Reply Last reply Reply Quote 0
        • H Offline
          Helper
          last edited by

          On 13/09/2014 at 03:57, xxxxxxxx wrote:

          Ok, thank you. Ive to think about. Didnt know that an argument isnt refreshed after calling second time.
          Solved that way

          import c4d  
          #Welcome to the world of Python  
            
          def A(lst=[]) :  
            l = lst[:]  
            l.append("A")  
            return l  
            
          def main() :  
            print A()
          
          1 Reply Last reply Reply Quote 0
          • H Offline
            Helper
            last edited by

            On 13/09/2014 at 04:09, xxxxxxxx wrote:

            why not just

            def A() :
                l = []
                l.append("A")
                return l
            

            or even

            def A() :
                return ["A"]
            
            1 Reply Last reply Reply Quote 0
            • H Offline
              Helper
              last edited by

              On 16/09/2014 at 05:00, xxxxxxxx wrote:

              Hi niklas,

              of course, you are right in this case. But I wanted to know how to handle lists as given argument.

              greetings
              rown

              1 Reply Last reply Reply Quote 0
              • First post
                Last post