1

I try to stick to the PEP8 coding conventions. I have a package called main. Inside the package there is a module called time, which has a class called Time inside. Now I have a bit of trouble finding a suitable name for my actual instance because time, which would be usually my choice, is already taken by the module and there seems to be a name-clash if I name it this way.

from main.time import Time

time = Time()
...
if time.status == main.time.STOPPED

Maybe I also placed the constant in the wrong module, but I thought that it would be better to have my constants at the place where they belong to. This is a constant used in my class Time (and the main module), so I can make sure that I don't mix it up with another constant called STOPPED used for player movement. Unfortunately I get an AttributeError: 'function' object has no attribute 'time'.

What would be the best solution here? Rename the constants to TIME_STOPPED and PLAYER_STOPPED and put them into a constants module? Naming my instance variable my_time or time_ or something like this is not really what I would like to do. What's the Pythonic way?

6
  • 1
    unclear. please provide full code and traceback Commented Oct 13, 2015 at 13:48
  • Your problem is that you don't have access to STOPPED, surely? I think you want from main.time import Time, STOPPED. You could also make it a Time class attribute, so the test would become if time.status == Time.STOPPED:, further reducing ambiguity with the player version of STOPPED. Commented Oct 13, 2015 at 13:49
  • Try adding an import like this: import main.time Commented Oct 13, 2015 at 13:51
  • I would like to have to write time.STOPPED instead of STOPPED, so that I won't get confused with player.STOPPED. The approach making it a class member sounds good to me - so I will either change my constants being inside of the class now, or rename them and put them into a constants module. Commented Oct 13, 2015 at 14:27
  • You should seriously consider using more verbose names, unless you are working in a very tight scope. Short names such as time are ambiguous (is it the current time, a time you are waiting for, ...) and also often used already - time is the name of a standard module. Commented Oct 13, 2015 at 14:54

1 Answer 1

3

Using the name time is a bad choice to begin with, not just because you already have a module that is named time, but also because there is a standard library module named time.

Anyways, this is not actually your problem (perhaps a clash with the STL module is, but you don't show enough code). The error AttributeError: 'function' object has no attribute 'time' means that main (in main.time) is a function, not module. Your line time = Time() is not the cause of this, but another function object called main inside your executable.

Sign up to request clarification or add additional context in comments.

3 Comments

main.time should be the imported module. I thought the nameclash is between that one (main.time.STOPPED) and the time instance of my Time object.. Anyways good point there - so I renamed the module to gametime and the class to GameTime. Nevertheless the problem stays the same if I use gametime = GameTime(). What are your names for instances.. Maybe I should use gametime_instance or game_time then to avoid the nameclash...
@freeDom- I can only reiterate that your naming of the Time or GameTime instance is questionable but not the issue. You have a function called main in your application's scope which shadows the main module.
Oh.. So thats the one? Yes, inside my __init__ module there I have a main function.. Maybe the module name main also is a very bad idea...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.