|
supster posted:This doesn't really make sense - you are implying that all functions in Python are virtual, but clearly it's working in the constructor. Is the __init__ function a special case and not virtual? When you write self.foo, it looks "foo" up in the object's __dict__. When you write Parent.foo, it looks "foo" up in the class' __dict__. In this case, __init__ isn't a special case because you're (by way of super) calling the "__init__" from the class' __dict__, not from the object's __dict__. The one in the object's __dict__ is from the last class that defined it, in this case, Child. You can sort of think of all methods being virtual. supster posted:If this is the case, is there specifically a different way I can access the base class's method that has been overriden? BaseClass.method(self) supster posted:If x was a method in the base class I think I could just call Parent.x(self, value) and be fine, but because it is an attribute I can't do that. Yes, but... The issue is that Parent.x doesn't exist. If we have an object 'p' of type Parent, then the __init__() method will add attribute x to p ("p.x"), but in this case, it doesn't change anything in the class' namespace, only the object's. Then, when you make an attribute x on class Child and call Parent.__init__(c) where c is an object of class Child, Parent.__init__ tries to set x on c, but since c is already a property (from the definition of class Child), it tries to call the setter. I wish I could explain this better. Sorry I'm not doing a terribly good job of it.
|
# ? Apr 6, 2009 21:48 |
|
|
# ? May 13, 2024 18:14 |
|
Benji the Blade posted:When you write self.foo, it looks "foo" up in the object's __dict__. When you write Parent.foo, it looks "foo" up in the class' __dict__. In this case, __init__ isn't a special case because you're (by way of super) calling the "__init__" from the class' __dict__, not from the object's __dict__. The one in the object's __dict__ is from the last class that defined it, in this case, Child. (Of course I am using the term "always" somewhat loosely here, I understand you can directly manupilate the dict and do other kinds of things.) Benji the Blade posted:The issue is that Parent.x doesn't exist. Benji the Blade posted:If we have an object 'p' of type Parent, then the __init__() method will add attribute x to p ("p.x"), but in this case, it doesn't change anything in the class' namespace, only the object's. Benji the Blade posted:Then, when you make an attribute x on class Child and call Parent.__init__(c) where c is an object of class Child, Parent.__init__ tries to set x on c, but since c is already a property (from the definition of class Child), it tries to call the setter. Benji the Blade posted:I wish I could explain this better. Sorry I'm not doing a terribly good job of it. supster fucked around with this message at 23:04 on Apr 6, 2009 |
# ? Apr 6, 2009 22:59 |
|
supster posted:So are you saying that "__init__" in a class's dict will always be that class's constructor and "__init__" in an object's dict will always be that object's parent class's constructor (if it inherited from one)? The object's __init__ will start out as the __init__ from the object's class (if that class defined __init__). You can access the parent class' __init__ (and similarly anything in their namespaces) by either using Parent.__init__ or super(Child, c).__init__, which in this case does basically the same thing, but performs some black magic if you're doing multiple inheritance. supster posted:Ok, I understand. So there's no internal instance of the parent object in the child object instance? I have no way of accomplishing what I am trying to do? Well, you could add a separate method or property that did the right thing, but then it wouldn't work with code that called the old property. You could instead use the adapter pattern to wrap the Parent rather than using inheritance, which I think is probably the right thing to do. There might be a better way to do it, but I'd need to give it some thought. I'm glad to hear it's been helpful. I promise once you get it, this is all a lot simpler than it sounds.
|
# ? Apr 6, 2009 23:20 |
|
Benji the Blade posted:You could instead use the adapter pattern to wrap the Parent rather than using inheritance, which I think is probably the right thing to do.
|
# ? Apr 6, 2009 23:30 |
|
supster posted:Right. Because x is not a class method, it's an object attribute so the class's dict doesn't know about it, only a Parent object's dict would. Attributes and methods are the same thing. Variables in Python aren't segments of memory that can be set to some value -- they are labels that point to other objects. The difficulty is that attributes/methods aren't separated based on their origin. You can't have "x" mean two different things in different methods, based on the method's class, without some black magic. I'm trying to understand the goal of your code, but failing. The code is failing because the parent's x value isn't assigned to until the call to the child's property setter is complete. If you'd like to have a default value, use something like: code:
|
# ? Apr 6, 2009 23:31 |
|
Janin posted:If that's not your intention, could you write what you're trying to do using a language you're more familiar with? Then we could show you the best way to do the same thing in Python. code:
code:
supster fucked around with this message at 23:55 on Apr 6, 2009 |
# ? Apr 6, 2009 23:40 |
|
Yeah, you can't do that. In Python, there's no such thing as casting, because an object has only one type. Parent objects aren't "nested" within children.
|
# ? Apr 6, 2009 23:47 |
|
Janin posted:Yeah, you can't do that. In Python, there's no such thing as casting, because an object has only one type. Parent objects aren't "nested" within children.
|
# ? Apr 6, 2009 23:54 |
|
supster posted:I realized that using casting didn't make my goal clear. I understand you can't cast in Python. I edited my source, take a look again and see if that makes more sense. I want x to be different when accessed from the context of the Parent class than when accessed from the context of the Child class. Python doesn't have contexts either. An object is the same, no matter what the code calling it thinks it is.
|
# ? Apr 7, 2009 00:00 |
|
Janin posted:Python doesn't have contexts either. An object is the same, no matter what the code calling it thinks it is. Are there any workarounds? I am only asking because I am curious, I would say that at this point designing around this limitation will almost always be a better solution than a hackish workaround. supster fucked around with this message at 00:08 on Apr 7, 2009 |
# ? Apr 7, 2009 00:05 |
|
supster posted:The reason I didn't want to do this was because the Parent class has another dozen or so attributes and methods that are being used, so I didn't want to have to have to change the usage of all of those to Adapater.Parent.method() or have to define all of those methods and attributes in my adapater class. Python helps you be lazy! code:
code:
Edit: Used slightly clearer variable names. Benji the Blade fucked around with this message at 16:48 on Apr 7, 2009 |
# ? Apr 7, 2009 16:43 |
|
Thanks! This is exactly what I was trying to do. Normally I would have acheived this with inheritance, but it is obvious that in Python an adapter class is the way to go. Thanks for making this clear for me.
|
# ? Apr 7, 2009 19:20 |
|
I've been told that it's better not to use methods named __like_these__ unless you really have to -- this adapter class seems kinda hacky, is it not?
|
# ? Apr 7, 2009 23:46 |
|
Captain Lou posted:I've been told that it's better not to use methods named __like_these__ unless you really have to -- this adapter class seems kinda hacky, is it not? __getattr__ is a "special method" that is already defined on objects anyway, Benji is just overriding it. __getattr__ just let's an object know what to do when something uses the dot operator on it, basically. foo.bar could be just as easily expressed: foo.__getattr__('bar') edit: actually you should never use __foo__ (both preceding and succeeding underscores) for a method name unless you're overriding an existing, but you should also "pretty much never" use __foo either, and should instead just use _foo No Safe Word fucked around with this message at 16:40 on Apr 8, 2009 |
# ? Apr 7, 2009 23:54 |
|
No Safe Word posted:__getattr__ just let's an object know what to do when something uses the dot operator on it, basically. Not to be pedantic, but foo.bar would be written foo.__getattribute__('bar'). __getattr__ only gets called for names not in foo.__dict__. * The names "__getattr__" and "__getattribute__" having different meanings has always rubbed me the wrong way, but regardless, they are useful. As far as not using names prefixed with __, No Safe Word is spot on, though overriding magic attributes (such as __getattr__) usually indicates there's magic afoot, which is sometimes warranted and sometimes a sign of being overly-clever. However, overriding methods like __init__ and __str__ and __iter__ are totally harmless and very often useful and even required. * To actually be pedantic, I believe __getattr__ gets called when __getattribute__ throws some exception or other to indicate that it can't resolve name. Usually this is because name isn't in __dict__, but if you override __getattribute__ too, well, god knows what kinda bullshit you've written that may or may not throw an exception.
|
# ? Apr 8, 2009 16:09 |
|
A general programming question in a specific Pythonic context: I have a webpage with a form (on a Plone site for what it matters), that when submitted calls an external program to do some calculations, grabs the results and renders them on the page. Simple. (For interest, it's matching submitted biosequences to an established corpus via various specialised commandline programs.) The problem has come about that sometimes the server is overloaded or the external program fails to complete, leaving my webserver waiting for an answer that never comes. So: what's the appropriate Pythonic idiom for handling this sort of situation? It seems reasonable to launch the external app, wait for x seconds and then conclude it has failed and return a "sorry" message to the user. Threads, job control, multiprocessing - what's the best way to it?
|
# ? Apr 8, 2009 16:23 |
|
Does anyone know of a good directory browsing module? I'm currently using FileDialog, but it's really lovely.
|
# ? Apr 9, 2009 04:04 |
|
he;lp I'm coding an agent-based simulation of a production system right now as a term project for a modeling/simulation class (NOT a programming class!), and made the glorious decision to simultaneously learn a new language AND object-oriented programming at the same time, and am having issues. I have extensive matlab experience, and am somewhat confused as to how python handles certain tasks. Apparently I'm confused about the intricacies of pass-by-value vs. pass-by-reference. Specifically, I have items that need to be passed from agent (person on factory floor) to agent and modified by the agents along the way. I have a class Item() which is inherited by the classes Part() and Assembly(), with Assemblies being multiple Parts joined together, and which look just like parts as far as agents are concerned. I need to be able to pass these items from agent to agent as well as keep track of all items that are in use. What is the best way to go about doing this? Can I/should I have a (global?) dictionary whose keys are the unique item ID number and whose entries are Items themselves, and pass only the ID numbers from agent to agent? If I pass an agent an Item in a dictionary or list, and have that agent modify an attribute of the Item, and then give it to another agent, is it still the exact same Item as in the list? Or would I be better off shuttling around the actual Items themselves, and tracking them some other way? Sorry if this is an inappropriate or confusing question, and thanks for any help!
|
# ? Apr 9, 2009 04:35 |
|
Spime Wrangler posted:Core Python has great explanations for objects, variables and memory in chapters 3 and 4, fortunately google books has the material available! edit: bah actually missing page 83 which has the beginning of the explanation.
|
# ? Apr 9, 2009 05:19 |
|
Ok, that explains it pretty well, I think. Thanks! Edit: ok yeah python loving rocks this poo poo makes so much more sense now It's so different from the way matlab works... I could get used to these "objects" and "pass-by-reference" and "pythonic" things. I decided to completely take the plunge and am also using VIM and linux for the first time and I'm just hitting the point where I feel it's worth it and no longer considering going back to windows. It's been a pretty solid brainfuck for the past few days. Reading through this thread has helped a lot, so thank you guys for doing this. Spime Wrangler fucked around with this message at 07:14 on Apr 9, 2009 |
# ? Apr 9, 2009 05:26 |
|
Spime Wrangler posted:Ok, that explains it pretty well, I think. Thanks! Welcome to the inner circle
|
# ? Apr 9, 2009 10:27 |
|
I'm using python with mod_wsgi, and I'm unsure if something I am doing is making my code "not thread safe": I am storing stateful (per request) data in a property in some of my classes. Does this mean that if I run a threaded server, that data could be shared between simultanious requests?
|
# ? Apr 9, 2009 17:01 |
|
That depends, are you storing data on the class itself, or just on instances? If it's on the class itself your data isn't threadsafe(unless you're dynamically creating classes per request), if it's on the instance the question is whether or not you're sharing the instances across requests.
|
# ? Apr 9, 2009 18:41 |
|
king_kilr posted:That depends, are you storing data on the class itself, or just on instances? If it's on the class itself your data isn't threadsafe(unless you're dynamically creating classes per request), if it's on the instance the question is whether or not you're sharing the instances across requests. Yea, I'm setting an instance as a property of a class, for every request (done in Django middleware to save me passing this drat variable back and forth between the views that have access to the request object and the models that don't; i broke mvc). So that isn't thread safe.. What if a module creates an instance of one of it's classes? (like at the end of django/contrib/admin/sites.py). I assume that isn't shared between threads?
|
# ? Apr 9, 2009 19:28 |
|
That is absolutely shared between threads, any global is.
|
# ? Apr 9, 2009 20:30 |
|
I installed python 3.0 on this system for some reason, and its for / range() syntax seems to hate me:code:
Edit: gently caress me, it's the print vs. print() syntax. Go IDLE, highlighting the line that *didn't* have the problem on it to confuse me. NightGyr fucked around with this message at 02:29 on Apr 10, 2009 |
# ? Apr 10, 2009 02:21 |
|
NightGyr posted:I installed python 3.0 on this system for some reason, and its for / range() syntax seems to hate me: http://docs.python.org/3.0/whatsnew/3.0.html#print-is-a-function
|
# ? Apr 10, 2009 02:29 |
|
NightGyr posted:I installed python 3.0 on this system for some reason, and its for / range() syntax seems to hate me: It does say line 2 ... I can't see the highlighting but ...
|
# ? Apr 10, 2009 05:44 |
|
king_kilr posted:That is absolutely shared between threads, any global is. What about storing global data as a property of the current thread? Something like this: code:
edit: just found out that threading.local() exists for exactly this purpose another edit: so Django already has django.utils.thread_support for this, but why does it go to such great lengths to implement it? It seems that I've left the GIL out of my example. Mashi fucked around with this message at 01:39 on Apr 11, 2009 |
# ? Apr 11, 2009 01:04 |
|
Yes, you can do that and it is threadsafe, however needing to do that is almost universally a sign of poor architecture of your application.
|
# ? Apr 11, 2009 01:27 |
|
EDIT: Nevermind, found this very helpful- http://oreilly.com/catalog/pythonwin32/chapter/ch12.html I've been using a private (yet commercial) API in Perl and Java for awhile. Their API is COM and CORBA. I have a desire to move some of this code to a web framework. I tried Ruby on Rails, but it doesn't work well with COM and other things I need to do. The idea of Jython intrigues me eventually, and I'm hoping to go with Django here. I'd like to do a proof of concept using Python and want to see if I can get Python to interact with the COM api correctly. I've attempted to google, but it's either a simple concept which doesn't get many hits, or I don't know how to word it. Given that Perl is a little more similar to Python than Java, I'll post some of my Perl code. I'm wondering if you could help me translate this to Python? code:
This is as far as I got, but I don't know what I'm doing. code:
mister_gosh fucked around with this message at 16:09 on Apr 13, 2009 |
# ? Apr 13, 2009 14:40 |
|
I feel like I should've seen something on the subject before, but I can't seem to find anything. Why is it that when you want to join a list of elements with a separator string, you call separator.join(a_sequence)? It never made any sense to me, and I always get it wrong because I think it makes more sense to call join on the sequence (i.e. seq.join(',')). I don't mean to start a holy war, I really don't care; I just think that maybe I'd remember it better if I knew why it's done this way.
|
# ? Apr 14, 2009 06:13 |
|
pokeyman posted:Why is it that when you want to join a list of elements with a separator string, you call separator.join(a_sequence)? str.join is better than tuple.join AND list.join AND my_derived_type.join AND ...
|
# ? Apr 14, 2009 06:27 |
|
That makes sense. I always got tripped up, as I thought of it as "why should the string handle putting itself between items of a sequence". I should've considered the implementation. Thanks!
|
# ? Apr 14, 2009 07:56 |
|
Another join question, can anyone tell me what is going on here:code:
Edit: Python's reporting the error from the source file not the code it's actually executing behavior strikes again. I was bouncing the wrong service to do my testing, and python was reporting errors in the fixed code... Zombywuf fucked around with this message at 12:00 on Apr 14, 2009 |
# ? Apr 14, 2009 11:44 |
|
I've been Doing The Right Thing and using setuptools to handle the installation of all the modules I write. But I recently wrote a few scripts, that I thought others might find useful. (They do conversion between various bioinformatic file formats.) When thinking about the best way to distribute it, I realised that setuptools would work fine. You can give it a version, that can be updated. It takes care of the prerequisites. It can copy a script to the right place. It does install an egg in the site-packages dir, but the egg appears to be non-importable. So it seems to work fine. Can anyone think of any objections or potential problems?
|
# ? Apr 14, 2009 13:03 |
|
I can't seem to get cx_Oracle to be recognized, and I've googled. I have Oracle 10.1.0.5 client (server is on the intranet) and Python 2.6 on my machine. I've tried installing multiple versions of cx_Oracle from SourceForge with the same result (4.x, 5.0 and 5.1 all for Python 2.6). All give me this error:code:
code:
code:
Any ideas?
|
# ? Apr 14, 2009 16:26 |
|
If I have an XML doc like this: <?xml version="1.0" encoding="UTF-8"?> <names> <name>John</name> <name>Mary</name> <name>Frank</name> <name>Steve</name> <name>Sally</name> </names> How would I go from the XML to getting each individual name as an element in a Python set? As in: set(['John', 'Mary', 'Frank', 'Steve', 'Sally']) At first I thought this would be simple given that Python is magical, but it's giving me hell. This could be because it's actually complicated (which I doubt), or because I'm a complete python noob. Any ideas?
|
# ? Apr 14, 2009 23:03 |
|
blitrig posted:If I have an XML doc like this:
|
# ? Apr 14, 2009 23:08 |
|
|
# ? May 13, 2024 18:14 |
|
blitrig posted:If I have an XML doc like this: http://docs.python.org/library/xml.etree.elementtree.html Parse the text into a document node, then use: set(e.text for e in doc.findall('name')) I don't have Python installed on this system, so this is untested mystes posted:XML + regular expressions No, never do this
|
# ? Apr 14, 2009 23:09 |