|
It's so strange I hunted down the change. It's so you can do this: code:
breaks fucked around with this message at 00:00 on Apr 16, 2017 |
# ? Apr 15, 2017 23:47 |
|
|
# ? May 15, 2024 03:31 |
|
So if I'm understanding this right, it lets me use self as a keyword argument and nothing more?
|
# ? Apr 16, 2017 00:00 |
|
That's my understanding from that bug, yeah, but I don't know any more about it than that. But if you look through the changes made in that patch, that is the one that removed the self argument from __init__.
breaks fucked around with this message at 00:06 on Apr 16, 2017 |
# ? Apr 16, 2017 00:00 |
|
Dominoes posted:PyCharm doesn't like to work with files unless they're in a folder that includes PyCharm meta files (.idea directory). Maybe that's it? Ya basically Most of my Python projects are small enough to not need auto complete and linting and grammar or however you say it and having to fire up Pycharm to do 150 lines of code isn't really worth it to me so I use vim instead
|
# ? Apr 16, 2017 06:56 |
|
There's a lot of slightly strange things in the collections module. Like docstrings using ''' instead of """, or single line doc strings using 1 quote instead of 3, or the whole implementation of namedtuple.
|
# ? Apr 16, 2017 09:44 |
|
Thanks for the responses guys. It was interesting to read about. I just downloaded PyCharm to give it a try because the refactoring tools looked really cool. How do I get it to work nicely with both .rst and .py files? I think it's treating my .rst files in my ./docs/source folder as Python files because the inspector keeps giving me warnings about missing docstrings or encodings etc.
|
# ? Apr 16, 2017 16:35 |
The first rule of namedtuple is 'don't look at the implementation of namedtuple'
|
|
# ? Apr 16, 2017 19:10 |
|
Fwiw attrs is a better namedtuple at the cost of needing a dependency
|
# ? Apr 16, 2017 22:38 |
|
IMO, typing.NamedTuple is also better than collections.namedtuple with no extra dependency. I haven't used attrs, though.
|
# ? Apr 16, 2017 23:27 |
Thermopyle posted:IMO, typing.NamedTuple is also better than collections.namedtuple with no extra dependency. I haven't used attrs, though. Thanks for pointing out typing.NamedTuple! It has a really cool syntax with support for type hints: Python code:
Eela6 fucked around with this message at 03:04 on Apr 17, 2017 |
|
# ? Apr 17, 2017 01:39 |
|
You are triggering me so hard with that unpaired open paren. E: Well, he was. I feel much better now. Proteus Jones fucked around with this message at 00:14 on Apr 18, 2017 |
# ? Apr 17, 2017 02:33 |
|
I need to be able to inject a function into a class when the object gets created, and I've found two ways of doing it: 1) I could play around with meta classes and make my metaclass do it for me (possible since I have all the inputs), or 2) I could use the "MethodType" class from the types module. I kinda like the second choice because it seems much easier and way more intuitive to be able to just do code:
But a lot of what I'm finding on the web suggest to use metaclasses without really saying why. Is there any practical differences to either way? e: I want the method injected to be bound to the object so I can't just assign it as an attribute. Boris Galerkin fucked around with this message at 22:10 on Apr 17, 2017 |
# ? Apr 17, 2017 22:05 |
|
I think what you are trying to do rarely makes sense. You should probably just write a method that calls whatever you want to call instead unless you have a really good reason not to. That said, why not just:code:
But again this situation should have every "there's got to be a better way" alarm in your head going off simultaneously. breaks fucked around with this message at 04:48 on Apr 18, 2017 |
# ? Apr 18, 2017 04:34 |
Boris Galerkin posted:I need to be able to inject a function into a class when the object gets created Why do you need to? Metaclasses are the preferred way of customizing classes, but most of the time you think you need them (or the even deeper magic of method injection) they are not necessary. To quote Tim Peters Tim Peters posted:[Metaclasses] are deeper magic than 99% of users should ever worry about. If you wonder whether you need them, you don't (the people who actually need them know with certainty that they need them, and don't need an explanation about why) Eela6 fucked around with this message at 05:06 on Apr 18, 2017 |
|
# ? Apr 18, 2017 05:04 |
|
Different question: does Continuum make money? What are the chances "conda install all_the_software_i_need" won't work in 2018 because Travis Oliphant has to choose between making it slightly easier for me to set up numpy or feeding his kids?
|
# ? Apr 18, 2017 11:53 |
|
Cingulate posted:Different question: does Continuum make money? What are the chances "conda install all_the_software_i_need" won't work in 2018 because Travis Oliphant has to choose between making it slightly easier for me to set up numpy or feeding his kids? 2018? I don't think anything is guaranteed 3 months from now.
|
# ? Apr 18, 2017 13:51 |
|
Cingulate posted:Different question: does Continuum make money? What are the chances "conda install all_the_software_i_need" won't work in 2018 because Travis Oliphant has to choose between making it slightly easier for me to set up numpy or feeding his kids? The nice thing about a heavily used open source project is that it cannot really go away. If Continuum disappears the code will still exist and there will be enough volunteers to maintain it.
|
# ? Apr 18, 2017 14:50 |
|
breaks posted:You should probably just write a method that calls whatever you want to call instead unless you have a really good reason not to. Eela6 posted:Why I have a class that represents this equation: In this equation the nu (Greek v looking thing) is a mathematical function, and there are different ways you can mathematically define it. The simplest mathematical function/model would be a constant value for it. More complex models use a varying amount of arbitrary parameters and it also depends on other parts of that equation that I showed. For example one model might need access to that "u" variable in the equation. Another model might need access to that "p" in the equation. Another one might need both. I know at runtime what model to use because I define it, but until then the class doesn't know what to pre-compute (I use lazy evaluation a lot here), what it needs to pass into the function, etc. The class representing that equation I showed knows all the methods to compute these things, though. So that's why I want to inject said function for nu into the object at runtime based on a single kwarg, so that it can access all of the class's methods. If there's a better way I'm all ears. I'd like to avoid creating a subclass of that equation class for every model I implement, because in actuality some of those other terms in the equation Boris Galerkin fucked around with this message at 15:04 on Apr 18, 2017 |
# ? Apr 18, 2017 15:00 |
|
can you do something like this:Python code:
|
# ? Apr 18, 2017 15:20 |
|
I also have a Pandas question. I have a multi-indexed Dataframe. I want to subset the DF based on the value of column A, and then look up stuff in the original DF based on values of one of the index columns and one of the regular columns in the subset, but in pairs. E.g., the subset is code:
code:
code:
huhu posted:2018? I don't think anything is guaranteed 3 months from now. SurgicalOntologist posted:The nice thing about a heavily used open source project is that it cannot really go away. If Continuum disappears the code will still exist and there will be enough volunteers to maintain it. Now a lot of the times, this will work out fine, somehow. But I'm sure if Continuum went under, things would change, and probably not for the better.
|
# ? Apr 18, 2017 16:22 |
|
I think the continued existence of whatever packaging system or repository is a hard problem. Continuum is just one company. PyPI (where your packages come from when you pip install something) is basically developed by one guy. Last I heard, it's infrastructure would cost 40 grand/month if Rackspace wasn't donating it.
|
# ? Apr 18, 2017 16:30 |
|
As a Python user on Windows, I'd put Christoph Gohlke in the same "I hope nothing ever happens to him" category. His "Unofficial Windows Binaries for Python" site has been a project lifesaver on more than one occasion.
|
# ? Apr 18, 2017 17:32 |
|
SurgicalOntologist posted:can you do something like this: I mean I could do this I guess. Your way looks like my way, except that in your way you would define every single model for "nu" inside of that class, and in my way I would define every model for "nu" inside a separate file called viscosity.py. I honestly thought I was being more clever by splitting those functions into their own file.
|
# ? Apr 18, 2017 17:36 |
|
That's reasonable too. In that case I would just doPython code:
Boris Galerkin posted:e: I want the method injected to be bound to the object so I can't just assign it as an attribute. which I don't follow. With the above it is bound to the object.
|
# ? Apr 18, 2017 18:06 |
|
Cingulate posted:Different question: does Continuum make money? What are the chances "conda install all_the_software_i_need" won't work in 2018 because Travis Oliphant has to choose between making it slightly easier for me to set up numpy or feeding his kids? onionradish posted:As a Python user on Windows, I'd put Christoph Gohlke in the same "I hope nothing ever happens to him" category. His "Unofficial Windows Binaries for Python" site has been a project lifesaver on more than one occasion. Dominoes fucked around with this message at 18:46 on Apr 18, 2017 |
# ? Apr 18, 2017 18:43 |
|
SurgicalOntologist posted:That's reasonable too. In that case I would just do Maybe I got my terminology wrong. I thought bound meant the thing where it implicitly gets "self" passed to it.
|
# ? Apr 18, 2017 18:45 |
You don't need to do any special magic to create a method and attach it to an object. So long as the first parameter is self it should 'just work'. Phoneposting so i cant go into more detail, but if you want an example I do so in my easyrepr implementation from earlier in this thread.
|
|
# ? Apr 18, 2017 19:00 |
|
Boris Galerkin posted:Maybe I got my terminology wrong. I thought bound meant the thing where it implicitly gets "self" passed to it. Eela6 posted:You don't need to do any special magic to create a method and attach it to an object. So long as the first parameter is self it should 'just work'. Phoneposting so i cant go into more detail, but if you want an example I do so in my easyrepr implementation from earlier in this thread. That's what I thought would happen (it would get self passed automatically), but I just checked and Boris is right, it doesn't. Huh. This is the first time in a while I've been surprised by Python. I guess you do need to do that thing with the types module (or use the first method I suggested). Edit: Eela6, IIRC about your easyrepr thing, it attaches to the class rather than an instance. In that case it works as expected.
|
# ? Apr 18, 2017 19:07 |
|
If the organizational issue is the only thing, just dump them in a class in the other file and inherit from it?
|
# ? Apr 18, 2017 19:45 |
SurgicalOntologist posted:That's what I thought would happen (it would get self passed automatically), but I just checked and Boris is right, it doesn't. Huh. This is the first time in a while I've been surprised by Python. I guess you do need to do that thing with the types module (or use the first method I suggested). Ah, I see! Thank you for the correction. It's a technical point but an important one.
|
|
# ? Apr 18, 2017 20:48 |
|
Boris Galerkin posted:I kinda like the second choice because it seems much easier and way more intuitive to be able to just do the first slide here is the quote i think of whenever metaclasses come to mind: http://www.vrplumber.com/programming/metaclasses.pdf as far as methodtypes goes, keep in mind functions have __name__ attributes too, so you can do stuff like: code:
|
# ? Apr 18, 2017 21:39 |
|
SurgicalOntologist posted:Huh. This is the first time in a while I've been surprised by Python. keep in mind what you're actually doing - assigning an object that exists outside the class to a value within the class: Python code:
|
# ? Apr 18, 2017 21:45 |
|
That is within the instance not the class. Functions in the class's namespace will work as methods (with maybe some exceptions if you really try to break it). I know that's maybe kind of pedantic but that's the distinction that changes the behavior so...
|
# ? Apr 18, 2017 21:56 |
|
breaks posted:That is within the instance not the class. Functions in the class's namespace will work as methods (with maybe some exceptions if you really try to break it). yeah i meant instance since i wrote the post right before it as a way to bind to instances, d'oh. just adding the function to a class' namespace still won't bind it, though
|
# ? Apr 18, 2017 22:10 |
|
Yes it will.Python code:
|
# ? Apr 18, 2017 22:13 |
|
actually wait a minute, i'm an idiot apparently! edit: been entirely too long since i looked at descriptor protocol stuff and somehow thought the same magic was needed for classes, i have no idea why later edit: well, that was actually worth googling again, since you can just use the function's descriptors directly: Python code:
Dex fucked around with this message at 22:29 on Apr 18, 2017 |
# ? Apr 18, 2017 22:16 |
|
You guys are right. I just tried that too and it works. The only problem is that then all Test objects would have access to that foo method. What I want to do is have foo he defined per object so that I could do something like code:
Dex posted:keep in mind what you're actually doing - assigning an object that exists outside the class to a value within the class: The way I thought of it was that everything is being passed around by reference/pointer. So when I assign test.foo = foo what I'm actually doing is saying "hey test object, your memory location corresponding to "foo" is now pointing to this other memory location which happens to have information on how to execute the foo function. I'm not sure if that's right or not but from a Fortran background it made perfect sense …
|
# ? Apr 19, 2017 00:15 |
|
How aboutcode:
|
# ? Apr 19, 2017 00:27 |
|
Boris Galerkin posted:What I want to do is have foo he defined per object the descriptor bit in my last edit is probably what you want if you need access to the instance(self), unless i've confused myself completely again? Python code:
output posted:foo Dex fucked around with this message at 00:31 on Apr 19, 2017 |
# ? Apr 19, 2017 00:28 |
|
|
# ? May 15, 2024 03:31 |
|
Which is a roundabout way of saying that Boris was right with his initial solution. From Hettinger's Descriptor HowTo code:
|
# ? Apr 19, 2017 01:40 |