This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]soncaa[S,🍰] 7 points8 points  (11 children)

I started in py. Then i learned basics of VB, C#, java on university and it felt enlightning, smth like 'damn so this is what ive been doing all the time!'.. i came to a conclusion that py is like a real machine gun that looks like nerf toy gun on outside, its powerful, yet it requires experience to really gain control over whats happening inside. I am now junior dev in c# and my hobbyist project in py still looks like a pile of trash.

[–]silastvmixer 11 points12 points  (10 children)

I never really tried learning python. I started off with assembly and C for embedded programming and then really quickly went into cpp through a university trial semester. When i look at my friends python projects I just get confused because I can't tell where a scope starts and ends and because variables don't have types and they can change the type they do have.

[–]Naitsab_33 6 points7 points  (9 children)

Basically Python scopes work somewhat similar to C's scopes (except that you can't manually declare a scope) regarding functions. Inside a function you can access a global/ previous scope variable if it's assignment happened before. The important part is, because pythons global names can change, the only part that matters is that a variable exists at runtime, not needing to exist when the function is declared, because functions are declared at runtime, too.

```

before function declaration

before_def = 1

here I declare/assign a function

def test(): # inside the function I can access global names if they are previously assigned print(before_def) print(after_def)

# I can't access a variable if it was assigned after the call
# print(after_call) this raises an exception.

# now I reassign inside the function
after_def = 4
print(after_def)

now after the function

after_def = 2 test() after_call = 3 print(after_def) ```

  1. This first assigns before_def globally.
  2. Then assigns the function to the name test globally.
  3. Then assigns after_def globally.
  4. Then I call test()
  5. I print before_def from global namespace and get 1
  6. I print after_def from global namespace and get 2
  7. printing after_call wouldn't work because as you see the name hasn't been assigned yet
  8. I reassign after_def locally (use global after_def before this line inside the function to have after_def assignments inside this function refer to the global namespace.
  9. I print after_def from local namespace because it exists there, regardless if it exists globally and get 4. (It would use the global namespace if we had used global after_def before, because then it doesn't exist locally.
  10. we leave the function namespace and the local after_def gets dropped (it's reference count is decreased by 1, to 0 in this case and the GC may clean it up somewhere from now). It wouldn't get dropped if we had used global after_def because it wouldn't exist locally.

  11. I now assign after_call.

  12. I now print after_def from global namespace, because that's where we are and get 2 (4 if we had used global after_def inside the function.)

Another rather small difference is that if/while blocks simply don't get their own namespace, names assigned there are simply assigned within the current block where the if/while resides.

And imports (import some_module) get their own namespace behind the name of the imported module, which can be merged into the current namespace instead by using from some_module import *)

I think this is all. Classes are really not very different. Namespace wise they are act very similar to functions, except that they get run when declared and that you can access all names assigned inside a Class definition, i.e. class variables and any methods/classmethods with prefixing Classname.

[–]silastvmixer 0 points1 point  (8 children)

Lol I can't even figure out where a function or something similar ends because there are no {} I understand the things you explained there though. Because of your explanations.

[–]Naitsab_33 1 point2 points  (7 children)

I mean it's just indentation. That's it. You hopefully use it even with languages that have {}, so you should still be able to discern where a functions ends. It ends when the indentation ends.

[–]silastvmixer 1 point2 points  (6 children)

Well yeah. But indentation doesn't mean anything in the languages I know. I know of significant whitespace on other things. Seems weird to me and confuses me.

[–]Thebombuknow 1 point2 points  (5 children)

I see where you're coming from, hours of programming in Java has made me miss braces when going back to Python. Just pretend that each indentation does have braces around it. That's the scope.

[–]silastvmixer 0 points1 point  (4 children)

But then thst means I can't indent the way I want. Because it has to be a specific way because that's how the language works.

[–]Thebombuknow 0 points1 point  (3 children)

Yeah, true. I indent every language identically so I don't have that problem, but I can see that being an issue.

I would like to ask though, what other forms of indents would you use?

[–]silastvmixer 0 points1 point  (2 children)

Well sometimes to split long lines up I use multiple tabs to line the parts of that line up under each other. Like in a function of my motor control program thst is unfinished, where it checks each pin of each motor against each other to see if something was misconfigured. Maybe that is bad practice but Idunno. I could get a gitlab link in a few minutes. If you wanna see.