1

I'm currently programming a game in python. I have a main class (engine.py), and many subclasses. The main class has some important variables like the player. When I now am in a subclass, and want to access another variable or function, it often looks like this:

self.handleToEngine.player.playerObj.animationHandler.doSomething()

Is there a way to shorten this? I don't believe the performance is that good, and it's no clean code.

1
  • You may have to provide more details on your class hierarchy and the composition of the objects. This will help us to suggest on how to goa about refactoring. Commented Feb 8, 2013 at 8:06

2 Answers 2

2

Give it a local name (also saves lookup time):

f = self.handleToEngine.player.playerObj.animationHandler.doSomething
f ()
Sign up to request clarification or add additional context in comments.

3 Comments

This is only usefull if I have multiple calls on this function, but I'm looking for something I could get a faster access with ..
@TobSpr Faster access during execution time?
Yeah, + less code to access ... but I believe there's no real solution
1

The attribute chain reflects your class structure, so of course you could refactor that if it makes sense. Also, in Python 2.2 or later, you can use __slots__ to improve access to member data, with some caveats.

1 Comment

I think I'm going to deal with this, even If I have to rewrite some structures ..

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.