Re: [Tutor] How do I add an argument too...
Well I use the getopt module. so in my foo.py it would be something like. import getopt try: opts, args = getopt.getopt(sys.argv[1:], U:u:P:p:H:h:?, [Username=, username=, Password=, password=, Help=,help=]) except getopt.GetoptError: print :nothing specila just an error sys.exit(1) for o, a in opts: if o in (-U, -u, --Username, --username): username = a if o in (-P, -p, --Password, --password): password = a if o in (-H, -h,--Help, --help,?): print foo help Hope the example helps. Jeff ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Re: Defining a function (Joseph Q.)
Brian van den Broek writes: Joseph Quigley said unto the world upon 2005-04-11 20:23: Well, now I've learned what def is good for. But what could I put in the parenthesis of def foo():? Of course self is always available, but what would maybe def foo(number1): do? An error right? So I now repeat my self, what else besides self could I put in there? Thanks, Joseph Hi Joseph, as Liam explained, you put the arguments (if any) to the function in the parenthesis. But, I want to address the bit where you said Of course self is always available. *Any* valid Python identifier (or name) can be used (is always available): def silly(a_name_I_picked_at_random): # well, not quite ... print a_name_I_picked_at_random # at random ;-) ... silly(42) 42 The name a_name_I_picked_at_random is like a placeholder inside the function for whatever input we gave to the function. And *that* can be any object: silly(silly) function silly at 0x01298070 Some functions require certain sorts of inputs in virtue of what they try to do with them, but we can still pass any sort of object in. For instance, Liam had: def foo(a, b): ... return a + b ... Now, try this: foo(42, '42') Traceback (most recent call last): File interactive input, line 1, in ? File interactive input, line 2, in foo TypeError: unsupported operand type(s) for +: 'int' and 'str' That's not a problem with Liam's function; it's a problem that I caused by sending it inappropriate inputs. At any rate, you are correct that you can always name an argument self. But self is, by *very* strong convention, used only within classes. Simplifying[*] a bit, a method is a function defined internal to a class. If you define a method in a class, you call the first argument of the method definition self, and it is automatically interpreted as a reference to the `active' instance of the class on which the method is called. So, don't use self outside of method definitions. You can (we are all adults here), but doing so will only confuse things given the strength of the convention. [*] The simplification comes in that not all methods in a class need to be bound to an instance, so not all methods employ self. But, my guess is that is something you shouldn't be worrying about just yet. Best, Brian vdB ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor Brian is correct. Also I would like to point out that def foo(*args): #this allows for any number of arguments to be passed. The *args is powerful when used correctly. *args is a list of any length of arguments being passed to the function and/or class, and the arguments passed can be any type/object you want to pass . So you might do something like. def foo(*args): print len(args) # this prints the length of the list of the arguements passed # so using that function you would see something like. in the example I only used basic types but you can pass objects as well. foo(1, 2, 3, cat, mouse, ['bee', 'honey', 'stinger']) 6 Notice that the length that is returned is only 6 but there are 8 items so it appears to have been passed to the function. In actuallity it is only 6 items. There is 3 integers, then 2 strings, then 1 list that is 3 items long. Thus the length of the list args is 6 items long. I wouldn't recomend passing mixed types like this to a function using args because it can get very confusing. I hope this helps you out and doesn't confuse you. I know from first hand experience the first time I saw the *args in some code I was lost. If you know the type of variables that are to be passed to the function and the number of them then it is easier to specify them, and use names. such as. def bar(num1, num2): #this is not catching invalid data types and such. return num1 + num2 #returns the sum of num1 + num2 bar(1, 2) 3 Also if you want to have default values passed incase the call is issued without any arguments passed. #This will take 2 arguments and add them together and return the sum # but the default values will fill in the remaining (missing) arguments. def my_foo(num1 = 0.0, num2 = 1.0): return num1 + num2 my_foo() 1.0 my_foo(6.7) 7.7 my_foo(2, 2) 4 The reason that my_foo() returns 1.0 is that no arguments were given so the default values were used and the 2nd argument default value is 1.0 and was added to the 1st argument's default value of 0.0. Thus 0.0 + 1.0 The reason that in the my_foo(6.7) example 7.7 is returned is that the default value of the 2nd argument is used since no 2nd argument was given was given. so it was 6.7 + 1.0 And Finally both arguments were specified my_foo(2, 2) the function takes both values and adds them giving you 4. I hope this helps, and does not make any more confusing. I try to be consise when giving information however I like to be thorough in my explanations. Jeff
[Tutor] Re: New to programming question
Ben Markwell writes: This is an exercise from How to think like a Computer Scientist. The following example shows how to use concatenation and a for loop to generate an abecedarian series. Abecedarian refers to a series or list in which the elements appear in alphabetical order. For example, in Robert McCloskey's book *Make Way for Ducklings*, the names of the ducklings are Jack, Kack, Lack, Mack, Nack, Ouack, Pack, and Quack. This loop outputs these names in order: prefixes = JKLMNOPQ suffix = ack for letter in prefixes: print letter + suffix The output of this program is: Jack Kack Lack Mack Nack Oack Pack Qack Of course, that's not quite right because Ouack and Quack are misspelled.* * *As an exercise, modify the program to fix this error. * == In trying to solve the problem I have come up with the following: prefixes = 'JKLMNOPQ' suffix = 'ack' xsuffix = 'uack' for letter in prefixes: n = 0 if prefixes[n] == 'O' or 'Q': print prefixes[n] + xsuffix else: print letter + suffix --- I know it doesn't work, but want to know if I am on the right track. And what is the solution? Thanks Ben ** Well you did come up with a way that would work sort of and you seem to be ont eh right track. I would make 1 small change if using your approach. prefixes = 'JKLMNOPQ' suffix = 'ack' for letter in prefixes: if letter == 'O' or letter == 'Q': print letter + 'u' + suffix else: print letter + suffix However there are other methodes to solve this problem but following the logic you are using this works. Jeff ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Re: Recursive list checking
joe_schmoe writes: Dear Pythonites I am looking for a more elegant solution to a piece of code that is too unwieldy and reptitive. The purpose of the code is for a new addition to a list to check whether it is a duplicate of a list element already a member of that list, and if so to regenerate itself randomly and to perform the same check again until such time as it is unique. For example, this is what I am currently doing: =code block # generate unique numbers and append to list nmbr01 = random.randrange( 1, 20 ) nmbr_list.append( nmbr01 ) nmbr02 = random.randrange( 1, 20 ) # check for duplicates and re-generate a number if needed while nmbr02 in nmbr_list: nmbr02 = random.randrange( 1, 20 ) nmbr_list.append( nmbr02 ) nmbr03 = random.randrange( 1, 20 ) while nmbr03 in nmbr_list: nmbr03 = random.randrange( 1, 20 ) nmbr.append( nmbr03 ) This method works, but increasing the numbers to be appended makes the code excessively long. I can't see anything in list methods that seems to do the trick, so anybody want to make a suggestion please? TIA /j ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor Well I would start by doing something like. nmbr_list = [] value = int(raw_input(Input the number of items you wish to generate)) for i in range(value): if i == 0: nmbr = random.randrange( 1, 20 ) nmbr_list.append( nmbr01 ) else: nmbr = random.randrange( 1, 20 ) # check for duplicates and re-generate a number if needed while nmbr in nmbr_list: nmbr = random.randrange( 1, 20 ) nmbr_list.append( nmbr ) I hope that helps. or gives you an idea. Jeff ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Re: Recursive list checking
Jeffrey Maitland writes: joe_schmoe writes: Dear Pythonites I am looking for a more elegant solution to a piece of code that is too unwieldy and reptitive. The purpose of the code is for a new addition to a list to check whether it is a duplicate of a list element already a member of that list, and if so to regenerate itself randomly and to perform the same check again until such time as it is unique. For example, this is what I am currently doing: =code block # generate unique numbers and append to list nmbr01 = random.randrange( 1, 20 ) nmbr_list.append( nmbr01 ) nmbr02 = random.randrange( 1, 20 ) # check for duplicates and re-generate a number if needed while nmbr02 in nmbr_list: nmbr02 = random.randrange( 1, 20 ) nmbr_list.append( nmbr02 ) nmbr03 = random.randrange( 1, 20 ) while nmbr03 in nmbr_list: nmbr03 = random.randrange( 1, 20 ) nmbr.append( nmbr03 ) This method works, but increasing the numbers to be appended makes the code excessively long. I can't see anything in list methods that seems to do the trick, so anybody want to make a suggestion please? TIA /j ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor Well I would start by doing something like. nmbr_list = [] value = int(raw_input(Input the number of items you wish to generate)) for i in range(value): if i == 0: nmbr = random.randrange( 1, 20 ) nmbr_list.append( nmbr01 ) else: nmbr = random.randrange( 1, 20 ) # check for duplicates and re-generate a number if needed while nmbr in nmbr_list: nmbr = random.randrange( 1, 20 ) nmbr_list.append( nmbr ) I hope that helps. or gives you an idea. Jeff ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor Err just noted that I screwed up on my idea.. here.. is a better party same idea edited some. nmbr_list = [] value = int(raw_input(Input the number of items you wish to generate)) # so this loops the value specified times, as this will add that number to list too. so a range of 1 - 20 in random order if 20 is the sepcified number. for i in range(value): if i == 0: nmbr = random.randrange( 1, value ) nmbr_list.append( nmbr ) else: nmbr = random.randrange( 1, value ) # check for duplicates and re-generate a number if needed while nmbr in nmbr_list: nmbr = random.randrange( 1, value ) nmbr_list.append( nmbr ) Jeff also feel free to ask me more speicifc questions via email if you think they won't help the cominity at all. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Might be a silly question!
Hello all, I am drawing a blank right now and can't seem to find anything on it and I am sure this issue has been addressed before, so here is the question. Can and if you can how do you set a variable as a constant? Example of what I mean: (this is loose and not python since variable type declaration is not needed in python) CONST int gamma = 5 This would mean in my little mind that the variable gamma which is an integer of 5 can not be changed in the code and would throw an error if you tried. Just wondering if there is a way of doing this in Python. This is just to clear this up in my mind it means nothing to my code since the way I write variable names any variable I want to remain unchanged I use caps to help distinguish easily. Thanks. Jeff They say the quickest way between to 2 points is a straight line, however where is the fun in that? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor