|
Anyone know the core reason why 'self' must be the first arg in any class constructor or class method in python? I can see one good usage: code:
I asked a guy I recently worked with and he said 'thats just how it is, deal with it'. Seaside Loafer fucked around with this message at 07:28 on Feb 19, 2012 |
# ? Feb 19, 2012 07:25 |
|
|
# ? Jun 12, 2024 05:29 |
|
There's retroactive reasons, such as the fact that it tears down the walls between functions and methods, and allows things like classmethods/staticmethods without language features... but the actual history is much simpler than that: "Modula-2 did it that way". That said, when you get to realize the simplicity of the mechanism, you'll see why it's a good thing. Also note that most languages work like this: in both .NET and Java (and ActionScript) the "this" parameter is passed as the first parameter so that they don't have to have two different opcodes for calling a method vs. a function, and as far as I know, all C++ implementations (all that count, gcc, clang, msvc++) do the same. Simplest path to enlightenment: Go ahead and implement a few decorators that can handle both methods and functions and then get back to me.
|
# ? Feb 19, 2012 07:55 |
|
Thanks man!
|
# ? Feb 19, 2012 08:00 |
|
This (I believe) comes from the early design of python - http://python-history.blogspot.com/2009/02/adding-support-for-user-defined-classes.htmlquote:A major design constraint was that I didn’t want to add syntax for methods that differed from the syntax for functions. Refactoring the grammar and the byte code generator to handle such similar cases differently felt like a huge task. However, even if I was successful in keeping the grammar the same, I still had to figure out some way to deal with instance variables. Pythons OO is really built around functions, rather than having methods. As a consequence, self is just another argument rather than a built-in. Other consequences are: code:
|
# ? Feb 19, 2012 08:25 |
|
Suspicious Dish posted:There's retroactive reasons, such as the fact that it tears down the walls between functions and methods, and allows things like classmethods/staticmethods without language features... but the actual history is much simpler than that: "Modula-2 did it that way". I don't think this is true - As far as I am aware, Modula-2 did not have objects/classes. Modula-3 did influence the module system some, but its class system is quite distinct from python's mechanisms. As mentioned in the link above, the retroactive reasons you suggest are in fact the primary motivating factors. quote:Simplest path to enlightenment: Go ahead and implement a few decorators that can handle both methods and functions and then get back to me. Also: Reimplementing the classmethod/staticmethod decorators is a good approach to understanding python descriptors, which are lovely
|
# ? Feb 19, 2012 08:35 |
|
I've never really done anything IO intensive work that uses os.path as well. I use the static blog generator blogofile, and I am trying to make sense of this tutorial. This is basically what my directory looks like, irrelevant folders not listed: _controllers \-- blog --- \-- __init__.py --- \-- minify.py static \-- css --- \-- style.css --- \-- foo.css --- \-- bar.css And when the controllers are run in the blogofile build command, I get this folder in the directory which is the generated static site: _site \-- (...) \-- static --- \-- css --- --- \-- style.css --- --- \-- foo.css --- --- \-- bar.css _controllers \-- blog --- \-- __init__.py --- \-- minify.py static \-- css --- \-- style.css --- \-- foo.css --- \-- bar.css I am trying to write a fairly simple controller that minifies my CSS files in static/css/ with cssmin that takes the CSS files there and minifies them, so I would ideally get this, when I build with blogofile: _site \-- (...) \-- static --- \-- css --- --- \-- style.css --- --- \-- style.min.css --- --- \-- foo.css --- --- \-- foo.min.css --- --- \-- bar.css --- --- \-- bar.min.css Right now, I do have a script, but nothing happens when it is run during the build. bf.config.css_path refers to a defined directory for my stylesheets at static/css, defined as os.path.join("static", "css"). code:
ufarn fucked around with this message at 16:28 on Feb 19, 2012 |
# ? Feb 19, 2012 16:02 |
|
Deliberately try to break you code and see what happens, or write some tests. Are you sure blogofile is actually calling the code?
|
# ? Feb 19, 2012 16:18 |
|
Maluco Marinero posted:Deliberately try to break you code and see what happens, or write some tests. Blogofile by default uses controllers to generate templates, not things like altered stylesheets and images, so I had to try to write it my own quirky way. I'll see if I can write something to log the behavior. EDIT: Added some basic comments to the code. ufarn fucked around with this message at 16:29 on Feb 19, 2012 |
# ? Feb 19, 2012 16:23 |
|
ufarn posted:I'll see if I can write something to log the behavior. http://docs.python.org/library/pdb.html pre:import pdb; pdb.set_trace() Better to use with a proper test framework though.
|
# ? Feb 19, 2012 16:34 |
|
tef posted:I don't think this is true - As far as I am aware, Modula-2 did not have objects/classes. Modula-3 did influence the module system some, but its class system is quite distinct from python's mechanisms. Ah, I did mean Modula-3. And while the class system is quite distinct, the name self appears right in the example. I can't find it now, but I remember reading a python-list post that said that the name "self" was chosen over "this" because Modula-3 used the term "self". Of course that's a slightly different question than what Seaside Loafer was asking, but I believe I'm well-grounded in saying that Modula-3 had at least some inspiration on the object system.
|
# ? Feb 19, 2012 17:04 |
|
Can someone explain why I can't get footnotes to work in Markdown?code:
code:
ufarn fucked around with this message at 16:47 on Feb 21, 2012 |
# ? Feb 21, 2012 16:44 |
|
Need the footnote at the bottom to be [^l] as well. Using brackets without the caret results in a reference link rather than footnote. http://freewisdom.org/projects/python-markdown/Footnotes Maluco Marinero fucked around with this message at 17:15 on Feb 21, 2012 |
# ? Feb 21, 2012 17:12 |
|
Maluco Marinero posted:Need the footnote at the bottom to be [^l] as well. Using brackets without the caret results in a reference link rather than footnote.
|
# ? Feb 21, 2012 19:05 |
|
I was wondering about GUI apps in python, all the tutorials I've seen seem to concentrate on commandline apps. I've done a couple of searches to see if there are any RAD IDEs as I'm used to delphi, but are apps developed in these then incompatible with standard python and each other? What I'm asking really is what's the most straightforward way to make guis, but also would using IronPython or Jython lead to me being annoyed 12 months down the road?
|
# ? Feb 22, 2012 22:11 |
|
Crankit posted:I was wondering about GUI apps in python, all the tutorials I've seen seem to concentrate on commandline apps. I've done a couple of searches to see if there are any RAD IDEs as I'm used to delphi, but are apps developed in these then incompatible with standard python and each other? wxPython is probably the most mature cross-platform GUI kit. That said, it's a bit of a mess, and doesn't follow most Python conventions (keep it constrained to its own class, and it should be fine). Documentation used to be godawful, but it's gotten much better over the last couple years. wxGlade is a graphical tool for building the wx code, but hasn't been updated in at least a year - there are probably much better ones out there now. http://zetcode.com/wxpython/ is a good place to start for a tutorial, much better than the official ones.
|
# ? Feb 23, 2012 02:04 |
|
Crankit posted:I was wondering about GUI apps in python, all the tutorials I've seen seem to concentrate on commandline apps. I've done a couple of searches to see if there are any RAD IDEs as I'm used to delphi, but are apps developed in these then incompatible with standard python and each other? In the past couple months I went through the same thing. I tested out most of the major frameworks and ended up using PySide. You can use QtDesigner with it.
|
# ? Feb 23, 2012 02:42 |
|
Hi, everybody. I have a question. I'm using Phylonet, a java program, to calculate the Robinson-Fould metric for phylogenetic trees. Via the command line, Phylonet can calculate the RF two ways: 1)By getting the trees from a file or 2)By writing the trees in the command window which requires three returns I wrote python code that does the first method: code:
|
# ? Feb 23, 2012 06:42 |
|
Crankit posted:I was wondering about GUI apps in python, all the tutorials I've seen seem to concentrate on commandline apps. I've done a couple of searches to see if there are any RAD IDEs as I'm used to delphi, but are apps developed in these then incompatible with standard python and each other? The documentation is decent. It works well and looks nice. It also has a designer that is light years better than wxglade. You can rapidly whip up a nice interface then code. It sure beats defining every element by hand. Also pyside follows python conventions a bit better than wxpython. I experimented with a simple, very lovely rss reader I made using ironpython,(using wpf for the GUI stuff) wxpython and pyside. Pyside used the least amount of ram. Also it was trivial to use pyInstaller to roll my program into a portable exe.
|
# ? Feb 23, 2012 09:49 |
|
Crankit posted:I was wondering about GUI apps in python, all the tutorials I've seen seem to concentrate on commandline apps. I've done a couple of searches to see if there are any RAD IDEs as I'm used to delphi, but are apps developed in these then incompatible with standard python and each other? So yeah, get wxWidgets, get a freeware design tool for it, play with it so you got some sample code, ditch the tool, go from there.
|
# ? Feb 23, 2012 10:07 |
|
Ither posted:
By returns I am assuming you mean you need to input things to STDIN and hit the return button. You will need to abandon the check_output() method and directly use a Popen object and the communicate() method. Something like: code:
|
# ? Feb 23, 2012 15:21 |
|
I wonder if there are an internals people that can tell me the types being used technically when passing a class around, not an object, and if I should be using type(class) instead. I'm trying to make this cooperate with Boost.Python, and I think I've stepped outside the normal domain of their cplusplus-sig mailing list. Say I have a list of objects implementing an interface, of which some could be a Python implementation. I was to expose a method that, given a class type, can determine if something in that list is that class specifically. Note--not that specific object! Say I have class Foo. If I were to do container.Has(Foo), I think I get something different from container.Has(type(Foo)). I figure the latter is normally what I'd want to use. It looks like the former is normally a Boost.Python.class and the latter comes up as "type" in Python in the signature, but it doesn't mate up with a C++ function that's using PyTypeObject*, so what is the underlying C structure for "type" in Python?
|
# ? Feb 23, 2012 16:52 |
|
OnceIWasAnOstrich posted:By returns I am assuming you mean you need to input things to STDIN and hit the return button. You will need to abandon the check_output() method and directly use a Popen object and the communicate() method. Thank you. I ended up solving the problem using temporary files, but I'll see if this method works too.
|
# ? Feb 23, 2012 17:58 |
I've decide to re-write a PHP webapp in Python since the PHP code is crap and I've been wanting to learn Python for awhile anyways. I was going to give Flask & SQLAlchemy a shot, seems to offer a bit more flexibility over something like Django. Any advice before I dive in?
|
|
# ? Feb 23, 2012 21:26 |
|
fletcher posted:I've decide to re-write a PHP webapp in Python since the PHP code is crap and I've been wanting to learn Python for awhile anyways. I was going to give Flask & SQLAlchemy a shot, seems to offer a bit more flexibility over something like Django. Any advice before I dive in? Flask is pretty cool. But so is Django, and you'll probably find it faster to get moving quickly under Django because some of the nice built-ins it gives you. Just be sure you're not falling into the trap of thinking that you'll need more flexibility without really thinking about what you're trying to do, because often times your problem isn't as hard as you'd think.
|
# ? Feb 23, 2012 21:42 |
|
Ok, I figure I'm doing something wrong with the style of how this should be coded, and I'm just having trouble googling the right keywords to determine the correct way to deal with this. So I wrote a class that contains multiple functions, but the ultimate goal of this class is to create a data file for me (using these multiple functions in the class). The code starts with something like this: code:
code:
The strange thing is that it seems to be half-working. For example, I am getting different output files, which are named as if they are for the different instances of var3. However, the output data that are written to them are all the same. My suspicion is that I'm doing something wrong with __init__. I also realize this is a fairly newbie question, so I'm sorry for how clueless I may be seeming right now.
|
# ? Feb 23, 2012 23:38 |
|
I've never used NumPy but it looks like what you're doing should work. Can you look closer at the way you're writing out files?
|
# ? Feb 23, 2012 23:58 |
|
JetsGuy posted:
Did you learn to program in C? Unless you have a good reason not to (and in the code you posted, you don't), you should iterate directly over the values in a container: code:
More generally, can you post enough code so that we can try to run this ourselves? What you posted looks like it'll work, so it's likely that there are problems lurking in what you didn't post.
|
# ? Feb 24, 2012 00:04 |
|
Hed posted:I've never used NumPy but it looks like what you're doing should work. Can you look closer at the way you're writing out files? ------ I originally had something to say about how I was looking there, it's happening slightly before that, as I've now found. Lysidas posted:Did you learn to program in C? Unless you have a good reason not to (and in the code you posted, you don't), you should iterate directly over the values in a container: Heh, I guess the first language I ever scripted in was Perl (shudder), and I haven't looked at it in 10 years. I pretty much taught myself python when I was in a computational physics course in grad school. I guess one issue that comes with that is that you learn how to code from people who pretty much live in FORTRAN and IDL. Also, the only real reason I do things like that is it's easier for me to think of arrays in terms of their indicies. I generally write huge arrays of data, and it's just easier for me to visualize when I'm writing routines. Yeah, I don't need it here though. quote:Also, learn you some PEP8. Ok, I'll take a look. quote:More generally, can you post enough code so that we can try to run this ourselves? What you posted looks like it'll work, so it's likely that there are problems lurking in what you didn't post. I can't post the whole thing, unfortunately. In any case, it would require a number of large .fits files to run. ********** However, I have found where the problem is occurring. The main() function within the class makedata calls another function that prepares the data to be written. What that function does is bins the data by time. Here's the snippet: code:
Here, self.Tbinning reports the correct value for each iteration run by the wrapper script. code:
FAKE EDIT: Oh, wait, it must be that t_details is remembering its value for the previous iteration from the wrapper, so it doesn't reset with the new Tbinning. Why would that be? Shouldn't it forget it that variable after the definition finishes? EDIT2: I "fixed" it by just having the main function intentionally pass new values for t_details to the T_bin function every time. I'm still curious as to why it wasn't able to pull them down itself through the class variables though. JetsGuy fucked around with this message at 01:12 on Feb 24, 2012 |
# ? Feb 24, 2012 00:47 |
|
JetsGuy posted:
I'm fairly sure you've hit a classic Python gotcha: mutable default arguments. The link explains it better than I can, but basically after multiple calls, t_details is no longer going to default to [None, None, None], but to whatever stuff you've assigned to it within the function. Try making t_details default to None (or require it be passed in), or not modifying t_details within that function, or even pass it as a tuple ( (None, None, None) ) which is non-mutable. I bet you'll see the results you're expecting.
|
# ? Feb 24, 2012 01:17 |
|
Harm Barn Gumshoe posted:I'm fairly sure you've hit a classic Python gotcha: mutable default arguments. The link explains it better than I can, but basically after multiple calls, t_details is no longer going to default to [None, None, None], but to whatever stuff you've assigned to it within the function. Whoa. This is very interesting, and really contradicts what I expected. As the page describes, I was operating under the assumption that all values under a defined function will be reset as they are local to that function. That is just very strange behavior to me that a list will retain its memory within the function. I get that it's a feature, but Indeed, the way I fixed it was to take out the default definition of [None,None,None], and just require that an array of the values I need are passed to it when the function is called. This fixed the problem.
|
# ? Feb 24, 2012 16:33 |
|
JetsGuy posted:Whoa. This is very interesting, and really contradicts what I expected. As the page describes, I was operating under the assumption that all values under a defined function will be reset as they are local to that function. The difference is that objects defined inside a function definition are not "under" that function. They're in the same scope (sort of) as the function itself.
|
# ? Feb 24, 2012 16:59 |
|
This is an interesting topic, and it motivated me to go hunt down the best explanation as to exactly why mutable default arguments aren't a good idea. It's been a few years! The best I've ever found is from Frederik Lundh: http://effbot.org/zone/default-values.htm. Short version: Default parameters are evaluated each time a function is *created* (via a call to "def"), rather than each time a function is *called*. And yeah, I realize this has been answered, but it's neat stuff anyway. NadaTooma fucked around with this message at 18:27 on Feb 24, 2012 |
# ? Feb 24, 2012 18:13 |
|
I just tried it:code:
|
# ? Feb 24, 2012 18:37 |
|
geonetix posted:I just tried it: My thoughts exactly - never run into it, can see that it could be useful, and it's definitely a POLA violation. Oh well, you really do learn something new every day.
|
# ? Feb 24, 2012 18:59 |
|
cool, I helped people learn something!
|
# ? Feb 24, 2012 19:29 |
|
Computer viking posted:My thoughts exactly - never run into it, can see that it could be useful, and it's definitely a POLA violation. Oh well, you really do learn something new every day. An example like this, a function which returns a list of the digits in an integer: code:
code:
code:
|
# ? Feb 25, 2012 09:50 |
|
Agreed, it doesn't seem like it solves any problems that couldn't be done equally well in a more explicit way, while it sets an unexpected trap (POLA and all that). Ah well, at least there's an implementation explanation for it that makes some technical sense.
|
# ? Feb 25, 2012 14:35 |
|
Computer viking posted:Agreed, it doesn't seem like it solves any problems that couldn't be done equally well in a more explicit way, while it sets an unexpected trap (POLA and all that). Ah well, at least there's an implementation explanation for it that makes some technical sense. This seems to come up a lot; perhaps it's worth mentioning in the OP?
|
# ? Feb 25, 2012 17:11 |
|
I guess I have a question. Has anything replaced PyGame? The site is in shambles and it's spammed and the wiki is being auto-trashed by bots advertising for stupid poo poo. It looks like Cocos2d is good and does a lot of the same stuff and is actually currently being worked on... is this what people are using now?
|
# ? Feb 25, 2012 17:16 |
|
|
# ? Jun 12, 2024 05:29 |
|
Look Around You posted:I guess I have a question. Has anything replaced PyGame? The site is in shambles and it's spammed and the wiki is being auto-trashed by bots advertising for stupid poo poo. It looks like Cocos2d is good and does a lot of the same stuff and is actually currently being worked on... is this what people are using now? Your options are likely PyGlet and PyOpenGL as alternatives, but both of those haven't had significant releases in the last year and it's altogether possible that they have no active developers, currently. If you have specific questions it might be worth checking the PyGame IRC channels and asking around.
|
# ? Feb 25, 2012 18:08 |