• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to improve readability of fields in classes?

 
Greenhorn
Posts: 3
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I'm quite new to Python and I was wondering - how to improve readability of fields in classes?

For example, let's take the following class

You may notice it has field "field" and then "self.field". First one is a static field. Second one is instance field. So, Java representation of it would be (even though it doesn't work):

In Python it creates some confusion mainly because fields can share the same name, and static field can be accessed using "self" (in Java we can access static fields using "this" as well, but at least IDE complains).

So, if the field is not used statically, then there is no point of declaring it as such in a first place. Which means that all instance fields have to be declared inside of __init__() which decreases readability (for me, I'm used to have all fields clearly visible and in one place).
SO, how can I declare fields such that they are easy to read, but don't create confusion as static fields do?
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Yorick Maloy wrote: . . . even though it doesn't work . . .

At least Java® prohibits two fields with the same name.

Don't do arithmetic with Math#random(). For everything except a double x such that 0.0 ≤ x < 1.0, use a Random object. By the way, your arithmetic won't compile because its result is a double not an int.

in Java we can access static fields using "this" as well, but at least IDE complains).

You mean some IDEs have a default setting to warn you of a potential mistake. I think Sun shouldn't have allowed us to call static fields by an object reference, because that can cause insidious and obscure errors. But they did, and we are stuck with it.

So, if the field is not used statically, then there is no point of declaring it as such in a first place. . . .

Don't think about fields being used statically or not. Think about fields belonging to the object, and static fields as the exception, which requires a good explanation.
But what is wrong with fields being declared in __init()__? It is different from Java®/C# but that is one of those things we have to get used to. You got used to randrange() quite easily, I see. We all need to be aware
 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Yorick Maloy wrote:'m quite new to Python and I was wondering - how to improve readability of fields in classes?


I'd think, name them the way so they reveal what they actually represent.

For instance, if the field is meant to represent an amount of apples, name it: amount_of_apples as opposed to field.

Another point about having global and instance fields of the same name. Simply don't do that. There rarely (if at all) should be a case that you'd want to have 2 variables of the same name, where one of those would belong to an instance, and another to a class.
 
Yorick Maloy
Greenhorn
Posts: 3
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote: Don't do arithmetic with Math#random(). For everything except a double x such that 0.0 ≤ x < 1.0, use a Random object. By the way, your arithmetic won't compile because its result is a double not an int.


Thanks for pointing that out!

You got used to randrange()

that was more of a lucky copy-paste
But what I don't like about declaring variables in a constructor is that passed variables are potentially changed during the construction (example will be below). There also can be certain operations between assignments. Which would mean that I should just declare variables to default values and only then set them.



Anyway, what do you think of the following implementation? Does it make any sense? Or would it better to avoid it?

Also, if I understood correctly, method overloading doesn't exist either. That's sad
 
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This points out a problem with Python versus Java. In Python, you can implicitly declare a (untyped) variable. Unlike in Java, where "this" is assumed when referring to unqualified names in member context, Python assumes you're working with a name in global or local scope. If you're lucky, it will complain about it.

I've been burned by failure to explicitly include "self" in expressions (and method declarations) more times than I can possibly count.

Incidentally, Facebook, like SalesForce has run into scalability problems with "git 'er Dun!" Python. It's all very well to be able to whip out pretty web pages fast, but as the codebase has expanded and aged, the true price of "productivity" has reared its ugly head. One of them (SalesForce, I think) has been converting to the "go" programming language. The other (Facebook?) has responded by developing a set of strict checking tools for Python code. Which means that instead of getting headed off by the IDE as you type in illegal datatype assignments and tests, that you get caught when you're about to check it all in. Or so I suspect, since they mentioned that running tests requires a significant start-up time.
 
Liutauras Vilda
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:...Python assumes you're working with a name in global or local scope. If you're lucky, it will complain about it.
...
I've been burned by failure to explicitly include "self" in expressions (and method declarations) more times than I can possibly count.


Now that you explained, I just understood the code I saw the other day. Was very surprised how variable declared within the if statement's body been used outside of it in some further part of the code. I thought somebody mistakenly messed up with an indentation level. So it appears such variable been dragged to a global context, assuming that part at runtime gets ever executed..

I appreciate the time I spend with compiled languages. Declared explicitly variables types for me aren't the boilerplate but readability properties, which as a bonus give less surprise at runtime.
 
reply
    Bookmark Topic Watch Topic
  • New Topic