Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
chips
Dec 25, 2004
Mein Führer! I can walk!
I haven't done much Python before, so I was playing around with reimplementing some PHP experiment code I'd made. What the PHP code did was use reflection to take a method of a class, discover its arguments, and then allow a http GET to call that method, using the GET arguments as arguments for the function.

I worked out how to discover the number of arguments of a method/function in Python, but it doesn't seem to have a way to discover the names of the arguments - anyone know if there would be a way to do this? I realize that in many languages, this probably doesn't make sense, since the names of arguments are just labels local to the definition of the function. If anyone has a nice guide on reflection in Python, it'd be appreciated - I could only find some rather obtuse references that led me to find im_func and so on, which I have to admit is slightly easier than PHP5's reflection methods.

Adbot
ADBOT LOVES YOU

chips
Dec 25, 2004
Mein Führer! I can walk!
That's great, thanks a lot.

chips
Dec 25, 2004
Mein Führer! I can walk!

GregNorc posted:

So I'm trying to create a function that returns True if the list is already orted, and false if it isn't.

This is how I planned to do it:
[code]test = ['a', 'c', 'd', 'b', 'e']
def is_sorted(list):
if list == list.sort()
return True
else:
return False

is_sorted(test)[code]

I did some tests in the interpreter and found the problem:


Nothing I've tried to fix this works. I really would like to store the result of list.sort in a list of its own, so I could just compare the two... if they're identical then the original must be sorted.

If anyone knows what I'm doing wrong, I'd appreciate a hint.

Wouldn't it be faster to check if the list is sorted in O(n) time (i.e. a single loop pass), rather than sorting it and comparing, taking O(n log n) time?

Also, calling a function requires (), otherwise you're just expressing the function object.

i.e. if you have a function def f(): return 1

a = f
b = f()

a contains the "value" of the function f, b contains the value 1.

chips
Dec 25, 2004
Mein Führer! I can walk!
That question does indicate that you should probably not be using the .sort() or sorted() functions, but rather loop through and check that each element is in order. It is more efficient to do it that way, though it would work with the given conditions to do your method. Also since your code will be in python and sort() is presumably in C, it might be slower for any reasonable number of elements anyway.

chips
Dec 25, 2004
Mein Führer! I can walk!
code:
santa = Santa()
d = {'cookies': santa, 'milk': santa, 'reindeer': santa}
So now every entry in the dictionary d points to the same santa object. Is that what you wanted? It might be useful to learn how Python names work.

chips
Dec 25, 2004
Mein Führer! I can walk!
How else would you express many-to-one? Having a "linked" mechanism doesn't add anything over references as you still need to be able to remap one of a currently linked set to an unrelated item. Any many-to-one collection must ultimately be a dictionary of references as I described, since you still have to explicitly specify each item.

Linking 'b' to 'a' is just

code:
d['b'] = d['a']
Then you can remap 'b' to something else later on

code:
d['b'] = Blah()
You could write a function (or a new method of a dictionary) which does something like

code:
def assign_many(d, xs, value):
    for x in xs:
        d[x] = value
So you can do (if it was a new method of a dictionary) d.assign_many(('cookies','milk','reindeer'),santa)

chips
Dec 25, 2004
Mein Führer! I can walk!
I think the point of the 2to3 tool is that you rework your 2.x code into forward compatible 2.6 code that can then be translated automatically to give valid 3.x code. So for example, as Scaevolus said, you'd need to make sure all your division operators are appropriate and that you aren't using any features that will be removed.

http://docs.python.org/dev/3.0/whatsnew/3.0.html#porting-to-python-3-0 explains the process pretty simply.

chips fucked around with this message at 18:18 on Jan 1, 2010

Adbot
ADBOT LOVES YOU

chips
Dec 25, 2004
Mein Führer! I can walk!
Maybe you need to wrap the javascript code in HTML comment tags, if that's allowed. Otherwise find a better parser.

  • Locked thread