Python Forum
Generator function in camel case
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Generator function in camel case
#1
Is it a good idea to name a generator function CamelCase?

I ask it is because a generator function is substitutable with an iterator class and class constructors are usually in CamelCase.

Otherwise, I would need to rename every time I switch between a generator function and an iterator class.
Reply
#2
If you have a consistent and documented code style, you can do whatever you want. If you want to reference PEP 8, which is what we default to on this forum, then you probably want to use snake_case for generators (which are basically functions). I'd consider it this way - if it's a type (class) that you can pass to isinstance, then use CamelCase, otherwise use snake_case (I give this advice for Python code, even though Python types like int and str are not CamelCase).

(Jul-16-2018, 08:28 PM)porton Wrote: Otherwise, I would need to rename every time I switch between a generator function and an iterator class.
I don't understand what you mean by this.
Reply
#3
(Jul-16-2018, 08:34 PM)micseydel Wrote: If you have a consistent and documented code style, you can do whatever you want. If you want to reference PEP 8, which is what we default to on this forum, then you probably want to use snake_case for generators (which are basically functions). I'd consider it this way - if it's a type (class) that you can pass to isinstance, then use CamelCase, otherwise use snake_case (I give this advice for Python code, even though Python types like int and str are not CamelCase).

(Jul-16-2018, 08:28 PM)porton Wrote: Otherwise, I would need to rename every time I switch between a generator function and an iterator class.
I don't understand what you mean by this.

I mean if I rewrite a generator function as an iterator class or vice versa, I need to change its casing.
Reply
#4
(Jul-16-2018, 08:37 PM)porton Wrote: I mean if I rewrite a generator function as an iterator class or vice versa, I need to change its casing.
As per PEP 8, I would change it.

A little more reason on why this might not be cargo-culty - as a client of your generator, I can assume that your generator is going to act like a normal Python generator. It won't have any additional methods (even if I want them) and if there's a bug I have a hint about where to start looking (there's not going to be any behavior overrides). If it's a class, I might want to look at the API more, if there are any behaviors I'm surprised by I will know that there might be more methods or behavior overrides involved, etc.

If you prefer following the logic that the name shouldn't change because the signature/interface didn't change, I'd just be sure to document that in your project. Especially if you're not confident that you can easily and completely do the rename, this seems like a reasonable time to violate PEP 8, which is a suggestion more than a rule.

Lastly, you can split the difference - name your thing as a class and then either wrap it with the function that returns an instance of your new generator class, or else include in the file it's imported from a line like
generator = Generator
which will allow existing clients to not have to be changed, potentially with a comment about the name being deprecated.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Switch case or match case? Frankduc 9 4,385 Jan-20-2022, 01:56 PM
Last Post: Frankduc
  Tuple generator, and function/class syntax quazirfan 3 3,755 Aug-10-2021, 09:32 AM
Last Post: buran
  list call problem in generator function using iteration and recursive calls postta 1 1,862 Oct-24-2020, 09:33 PM
Last Post: bowlofred
  basic random number generator in replace function krug123 2 2,005 Jul-31-2020, 01:02 PM
Last Post: deanhystad
  Generator function for even numbers mp3909 4 5,926 Mar-21-2020, 07:40 PM
Last Post: buran
  how to make my function be a generator Skaperen 2 1,994 Jan-27-2020, 01:07 AM
Last Post: Skaperen
  generator function that yield from a list buran 9 4,101 Jun-04-2019, 10:26 PM
Last Post: snippsat
  receive from a generator, send to a generator Skaperen 9 5,422 Feb-05-2018, 06:26 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020