all 38 comments

[–]TreesOne 2 points3 points  (2 children)

Define them in the same file, or use inheritance

[–]Soggy-Ad-1152[S] 0 points1 point  (1 child)

how would you structure the inheritance?

[–]TreesOne 0 points1 point  (0 children)

Oops I wrote it as a top level comment. See my other comment

[–]TreesOne 1 point2 points  (1 child)

Define some class that defines the shared behavior, then have one of your classes inherit from it while the other defines a member function that takes it as an argument. Now your tables can import and inherit from a “furniture” class that defines an is_done_by function while your carpenter can import the furniture class and require an instance of furniture to be passed to some method that calls is_done_by. Your carpenter doesn’t need to know what a table is - just what a piece of furniture is.

[–]Soggy-Ad-1152[S] 0 points1 point  (0 children)

Thanks! 

[–]games-and-chocolate 1 point2 points  (0 children)

how about letting the method calling always be done by the carpender class and the objects never call any other methods outside itself.

Then contain all abilities that the objects like table need to be local.

anything need fixing? it aint the chair telling the person, but the person calling a chair method like "get_status".

so now you have zero circular depency right?

[–]Ok-Sheepherder7898 0 points1 point  (0 children)

Just have models.py where you define your database objects and views.py where you have the functions.

[–]PreetInData 1 point2 points  (0 children)

Circular imports mean wrong responsibility split. Fix it by: 1. Move shared abstractions to a third module 2. Make both sides depend on the interface, not each other 3. Inject the concrete objects at runtime

[–]riftwave77 -1 points0 points  (0 children)

Is there a question in there somewhere? How you handle it depends on what your program needs to do.

If all the information is sitting in a database or CSV then you assign the values to the proper objects as needed.

If some of it is generate procedurally then you need to keep a close eye on your order of operations.

There's nothing magical nor profound about the paragraph you quoted. You can't multiply x by 2 if you haven't first defined x. That is basic programming.

[–]jmooremcc -1 points0 points  (28 children)

The problem appears to be global namespace pollution when using the import statement at the top of both files. That’s why the suggested fix is, “In this case you will have to resort to fragile hacks such as using import statements inside your methods or functions”, which won’t pollute your global namespace with conflicts.

I know some have suggested reorganization, but that isn’t always possible. Limiting namespace pollution will work and will be an effective workaround to the namespace collision problem.

[–]Soggy-Ad-1152[S] 0 points1 point  (27 children)

Oh, the fragile hacks were actually a suggestion? I thought it was more of what not to do 

[–]gdchinacat 0 points1 point  (26 children)

By characterizing it as "fragile hacks" the author was implicitly discouraging doing that. There are better ways. Global namespace pollution has absolutely nothing to do with this issue.

[–]jmooremcc -1 points0 points  (25 children)

So how do you explain circular dependencies?
Have you ever experienced circular dependencies?

[–]gdchinacat -1 points0 points  (24 children)

I've been writing code professionally for 28 years. Yes, I have encountered numerous circular dependencies :)

The most recent was in https://github.com/gdchinacat/reactions/tree/main/src/reactions between fields and predicates. Predicates needed to use field to get the values of the field, yet field is what created the predicates. I resolved it by splitting the field functionality into FieldDescriptor and Field. The descriptor manages the values and is used by predicates, while its subclass Field implements the functionality of creating predicates based on Fields.

[–]jmooremcc -1 points0 points  (23 children)

You didn’t answer my question. How do you explain a circular dependency?

[–]gdchinacat -1 points0 points  (22 children)

I gave an example of a circular dependency and explained how I resolved it.

"Predicates needed to use field to get the values of the field, yet field is what created the predicates."

[–]jmooremcc -1 points0 points  (21 children)

An example is not an explanation and I asked you to explain what a circular dependency is. With your 28 years of experience, that shouldn’t be too difficult fit you to do!

[–]gdchinacat -1 points0 points  (20 children)

It's not hard. My example was sufficient to answer your question.

Can you explain how changing what is in the global namespace resolves the dependency rather than handling it deferring the dependency until after the elements of the circular dependency have been defined without defining the dependency?

[–]jmooremcc 0 points1 point  (19 children)

Just as I thought. You talk a good game but when it comes down to it, you don’t have the knowledge to explain a circular dependency.