Why this code is working?

H

Hussein B

Hey,
Why this code is working?
.... x = 88
.... f2(x)
........ print x
....88

Thanks.
 
B

Bruno Desthuilliers

Hussein B a écrit :
Hey,
Why this code is working?

... x = 88
... f2(x)
...
... print x
...
88


Well... Because it is correct ?

What make you think it _shouldn't_ work ?
 
H

Hussein B

Hussein B a écrit :


Well... Because it is correct ?

What make you think it _shouldn't_ work ?

Because def2 is defined after def1 in an interpreted language, not
compiled.
 
P

Peter Otten

Hussein said:
Why this code is working?

... x = 88
... f2(x)
...
... print x
...
88

The name 'f2' is not resolved once when the f1 function is created. Instead
Python looks for a global name 'f2' each time f1 is executed.

Peter
 
S

Steven D'Aprano

Because def2 is defined after def1 in an interpreted language, not
compiled.

Python is compiled.

What do you think the c in .pyc stands for? And what do you think the
compile() function does?

It's just not compiled to machine code. It's compiled to byte code.
 
H

Hussein B

Python is compiled.

What do you think the c in .pyc stands for? And what do you think the
compile() function does?

It's just not compiled to machine code. It's compiled to byte code.

Yes I know Python programs can be compiled but when I do:
python Script1.py
Did Python compile Script1 into the memory?
 
R

r

Listen Hussien,
In python you do not have to worry about what order this is, what type
to declare that, my friend your bonds are cut. Be happy with your
freedom and stop complaining about it. I guess where i come from
freedom is second nature so i took to python pretty quick.
 
D

Diez B. Roggisch

r said:
Listen Hussien,

Granted, with a name of "r", spelling it wrong is hard, and thus you might
not be trained in the art of spelling names proper. But I suggest you try
your best. After all, you posts lack so much in content, you could at least
excel in form...
In python you do not have to worry about what order this is, what type
to declare that, my friend your bonds are cut. Be happy with your
freedom and stop complaining about it. I guess where i come from
freedom is second nature so i took to python pretty quick.

What's that condescending nonsense to mean? What do you know of the
heritage and living circumstances of the OP? And since when is a choice of
typing-system correlated with the political and social aspects of a
society - you care to prove that?

And where did the OP "complain"? He merely wondered.

I'm well aware that you are a piece of trolling d.....t. But this goes a bit
to far, Mr.

Diez
 
A

Aaron Brady

Granted, with a name of "r", spelling it wrong is hard, and thus you might
not be trained in the art of spelling names proper.

Or spelling proper names! Bah ha.
 
B

Bruno Desthuilliers

Hussein B a écrit :
Because def2 is defined after def1 in an interpreted language, not
compiled.

CPython actually compiles to byte-code, which is then executed.

But anyway: even if it was fully interpreted, the fact that f2 is
defined after f1 should not matter - what matters is that name f2 exists
(and is bound to a callable taking a single mandatory argument) when f1
is actually _called_.
.... x = 42
.... f2(x)
....Traceback (most recent call last):
File "<stdin>", line 1, in <module>
.... print x
........ print "pikaboo"
....Traceback (most recent call last):
File "<stdin>", line 1, in <module>

HTH
 
B

Bruno Desthuilliers

Hussein B a écrit :
(snip)
>
Yes I know Python programs can be compiled

wrt/ CPython: s/can be/are/
but when I do:
python Script1.py
Did Python compile Script1 into the memory?

Yes. Only (imported) modules are automatically saved as .pyc.
 
M

Mel

Hussein said:
Because def2 is defined after def1 in an interpreted language, not
compiled.

You don't have to know about f2 when f1 is compiled. You only have to know
about f2 when f1 is called.

I predict that

def f1():
n = 88
f2 (n)
f1()
def f2 (x):
print x


will *not* work. Python won't consider the non-definition of f2 to be a
problem until you try to actually use it.

Mel.
 
S

Steve Holden

Diez said:
Granted, with a name of "r", spelling it wrong is hard, and thus you might
not be trained in the art of spelling names proper. But I suggest you try
your best. After all, you posts lack so much in content, you could at least
excel in form...


What's that condescending nonsense to mean? What do you know of the
heritage and living circumstances of the OP? And since when is a choice of
typing-system correlated with the political and social aspects of a
society - you care to prove that?

And where did the OP "complain"? He merely wondered.

I'm well aware that you are a piece of trolling d.....t. But this goes a bit
to far, Mr.
I am happy to say I did not receive the post of which you complain,
Diez. Its tone was indeed abominable, and is most definitely unwelcome
within fifteen thousand miles of this list. Just in case anyone else
thought it was OK to make irrational deductions based only on someone's
name ...

regards
Steve
 
R

Roger

I guess where i come from
freedom is second nature so i took to python pretty quick.

Wow, that's presumptive, assuming, and harsh. Not at all helpful and
unnecessary. Geez...
 
R

r

Yes promoting freedom, claiming that freedom is a good thing, and not
being afraid to open my mouth about it, fight for it, free others from
their bonds, makes me free.

No presumption was made about the OP, the only "meaning" in my message
was to free the OP from his self imposed bonds while using Python. You
made this political. Seems like you have a predisposition of hate
towards me, maybe it is unconscience and you do not realize it, but i
highly doubt it.

Come down from your tower of righteousness Deiz, loosen up that stiff-
upper-lip and bring your brown-nosing minions with you!
 
A

alex23

[...] *not being afraid to open my mouth about it, fight for it, free
others from their bonds* [...]
Come down from your tower of *righteousness* Deiz, loosen up that stiff-
upper-lip and bring your brown-nosing minions with you!

(emphasis mine to highlight the humorous hypocrisy)

Let's see, one poster consistently posts helpful and usable coding
advice...the other is on some bizarre, self-aggrandising jihad. I
wonder to whom people in this group will actually pay attention...

And let's face it, r, the only reason you're lashing out at Diez about
his 'minions' is from your complete inability to gain any of your own
in your misguided crusade.

Done any coding lately? How's that Python bridge to SketchUp coming
along? It's been months already, surely you've invested as much effort
into learning Python as you have in talking about it?
 
R

r

How's that Python bridge to SketchUp coming
along? It's been months already, surely you've invested as much effort
into learning Python as you have in talking about it?

Thanks for asking Alex23,
The ball is rolling on the Python SketchUp integration as we speak,
many people are very excited to start coding in Python. Would you care
to join our pursuits alex?
 
R

r

Actually Alex, i have not stopped working on getting Python into SU in
one form or another since that crazy post of mine almost 3 moths ago.
I have many people in the SU community asking me almost daily when i
will get finished. I have a lot of work to do at this point but i will
keep fighting because i believe Python will greatly benefit all SU
users.

1.) Python has tons and tons of well written tutorials aimed at Non-
programmers.
2.) Python is easy to learn making it the perfect choice for any API
3.) Python has an easy to use built-in GUI (Tkinter)
4.) Python allows for true procedural and true OOP programming. OOP
can sometimes trip up a new programming student.

I feel it is much easier to learn procedural coding and then move into
OOP. OOP is not really hard to comprehend, but learning to program and
at the same time dealing with the abstraction of OOP is a little much
to take on. And since an API will be utilized mostly by non-
programmers/intermediant as well as pros, Python lends itself as the
best option available today for all skill levels!

Once i finally get Python interpretor into SU, i plan to write a
tutorial (free of course) specific aimed at SU users who want to
script. Not only will this cover Python Programming but will
completely cover the Python SU-API.

SU is exploding daily, group membership is @ 14000+ and spam is almost
nonexistent. This application is truly revolutionary and we must get
Python in before it is too late. I have talked with a members of the
SU DEV team and he would love to see Python scripting in SU. He also
told me they have kicked around the idea of other languages in the
past, so Python would be a prime choice.

SU is changing the world of modeling the way Python changed the world
of programming.I hang out quite a bit in the Google SketchUp Help
Groups guiding new users. During this time i have seen many people
stuggle with scripting the application. This is where i truly believe
Python can fill a void and shine.

The combination of Sketchup's intuitive and ground-breaking UI,
coupled with the common sense approach and simplistic elegance of
Python will be an unstoppable force. Not only will this expand Python
and Sketchup's influence, but will give a SketchUp user the complete
power they need over the application -- in the quickest, and most
painless way.

....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,299
Messages
2,571,544
Members
48,295
Latest member
Adriene271

Latest Threads

Top