khanna-lab / cadre

Other
0 stars 0 forks source link

Code convention: [module_name].[class_name]() #13

Closed khanna7 closed 2 years ago

khanna7 commented 2 years ago

https://github.com/khanna7/cadre/blob/d1e86278bf0b9108557d01e4ebf1cf04ff4f7542/python/src/cadre_model.py#L42

@ncollier: Stylistically, is it weird to have something like this Person.Person()...? This is happening because the Person() class is in a file called Person.py. Is this bad practice?

dsheeler commented 2 years ago

I don't think it's weird, honestly. But there is a convention that we follow in p2m4py and repast4py and seems to be industry standard: module names use lowercase letters and _ to separate words, and python classes always start with a capital letter and are camel case. So you'd end up with person = person.Person()

You could look at a smattering of .py files in there and see the class names inside. https://github.com/UChicago-CCHE-Modeling/p2m4py/tree/main/p2m4py

ncollier commented 2 years ago

I think it's fine. You can see similar patterns in built-in python packages -- datetime.datetime for example.

See

https://peps.python.org/pep-0008/#prescriptive-naming-conventions

for a useful style guide.

khanna7 commented 2 years ago

Thanks so much! So an instance of the object (i.e. a single person) and the name of the module are lower case, and the class name is upper case: Person(). This will end up being something like this:

person = person.Person()?

On Fri, Jun 17, 2022 at 9:27 AM Nick Collier @.***> wrote:

No, I think it's fine. See

https://peps.python.org/pep-0008/#prescriptive-naming-conventions

for a useful style guide.

— Reply to this email directly, view it on GitHub https://github.com/khanna7/cadre/issues/13#issuecomment-1158871204, or unsubscribe https://github.com/notifications/unsubscribe-auth/AB6QUBHBNNNRQ7CSD5H7RPTVPR4L5ANCNFSM5Y73TUPQ . You are receiving this because you authored the thread.Message ID: @.***>

ncollier commented 2 years ago

I think with person = person.Person() you might be shadowing the person module with a variable named person. So your next call to person.Person() might use the person variable rather than the person module. I'd have to test this to be sure though.

To get around this you could do p = person.Person() or something like

import person as prsn

person = prsn.Person()
khanna7 commented 2 years ago

Got it! Thanks guys. I changed the name of the module from Person.py to cadre_person.py to follow the repast convention and avoid the shadowing issue @ncollier mentioned.