Giter Club home page Giter Club logo

Comments (4)

jacanchaplais avatar jacanchaplais commented on July 18, 2024

Ah, so I've been poking around the test files, and found this:
https://github.com/scikit-hep/pyhepmc/blob/main/tests/test_from_hepevt.py#L17
So I gather that it's not actually a constructor classmethod.

The naming here is a bit confusing, as .from_xxx() always implies to me it's a classmethod, rather than an instance method which does some inplace operation.

Anyway, I'm sorted now, thank you! (I'll leave open in case you want to add some more docs for numpy stuff etc.)

from pyhepmc.

HDembinski avatar HDembinski commented on July 18, 2024

Sorry for the late reply. I agree that it would be more pythonic if from_hepevt was a classmethod. I am not sure if there is a way to fix this at this point, but if it is possible to have both a classmethod and a method with the same name then I am happy to fix this. The potential classmethod from_hepevt then needs two additional optional arguments momentum_unit and length_unit.

Perhaps import_hepevt would have been a better name?

The numpy-friendlyness is discussed here: https://scikit-hep.org/pyhepmc/examples/processing.html

from pyhepmc.

HDembinski avatar HDembinski commented on July 18, 2024

For the record, this works

class Foo:
    def from_bar(*args, **kwargs):
        if isinstance(args[0], Foo):
            print("method", args[1:], kwargs)
        else:
            print("classmethod", args, kwargs)

from pyhepmc.

jacanchaplais avatar jacanchaplais commented on July 18, 2024

Sorry I missed this! I can see from the docs on the numpy stuff, it's mainly regarding computation rather than IO, which is a bit different to my use-case, but cool neverthless. :)

Also, fun example! Though, I think this would be closer to accessing it as a staticmethod? Decorated classmethods receive the class type constructor as the first argument, whereas if you were to do

Foo.from_bar("spam")

The first argument here would be "spam", not the Foo constructor. So, it seems more like a staticmethod, to me. I guess you could still use it as though it were one, but as in your example, you would need to specifically point it to Foo. Probably that's not an issue, unless you subclass it. :)

from pyhepmc.

Related Issues (14)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.