Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Python Newbie

Reply
Thread Tools

Python Newbie

 
 
Dave Angel
Guest
Posts: n/a
 
      02-24-2013
On 02/24/2013 10:46 AM, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Hi guys,
>
> Question. Have this code
>
> intX = 32 # decl + init int var
> intX_asString = None # decl + init with NULL string var


None is not a str, and it's not a "NULL string var" Perhaps what you
want is intX_asString = ""

> I am using a Python IDE called PyScripter. Its Intellisense is full
> of methods starting and ending with "__", hence the question.


I'm surprised; I'd expect the Intellisense to filter those out by
default, since people seldom should call them.

--
DaveA
 
Reply With Quote
 
 
 
 
Joshua Landau
Guest
Posts: n/a
 
      02-24-2013
On 24 February 2013 22:08, Chris Angelico <(E-Mail Removed)> wrote:

> On Mon, Feb 25, 2013 at 8:35 AM, Joshua Landau
> <(E-Mail Removed)> wrote:
> > def solve_quadratic(a, b, c):
> > """Solve a quadratic equation of the form ax˛ + bx + c = 0
> >
> > The result will be a tuple of the two results; the results can be equal

> if
> > the determinant is 0.
> > This supports imaginary results for if the determinant is negative."""
> > ...
> > results = [top/(2*a) for top in fraction_tops]

>
> Yeah, I think we know which one is the more readable... Just to
> nit-pick a little though, that returns a list when its docstring says
> it'll return a tuple
>


Good catch.


> Other than that (which is probably better solved by changing the docs
> than the code), the only change I'd make would be to ditch the
> fraction_tops temporary (and to throw out some of the comments that
> serve only to reexpress the code that immediately follows them, though
> for a demo they're entirely appropriate).
>


I knew someone would critique it. It's an exaggerated demo for foo's sake.
Heck, who even uses a function like that (or uses unicode in comments )?

 
Reply With Quote
 
 
 
 
Joel Goldstick
Guest
Posts: n/a
 
      02-24-2013
On Sun, Feb 24, 2013 at 5:43 PM, <(E-Mail Removed)> wrote:

> Josh,
>
> Not thank you for your malicious post.
> I think you are missing the point here.
>
> My source code was just a dummy to offer context for the question I wanted
> to ask. Further down the line, if I ever feel I don't need to
> pseudo-declare variables I will stop doing it. But for the moment I am
> trying to imitate familiar ground.
>
> My code as written has no syntax errors, so what's the problem? It is
> highly unlikely you will ever read any of my Python code - no need to get
> excited over a few of my lines.
>
> And you don't need to answer questions which were not posed, thank you.
>
> I wanted Python to register what type of variable I'm after. So I init my
> vars accordingly, int might be 0, float 0.0 and string with null, err...
> None.
>


In a language that defines Names that are bound to objects, there can't be
a 'type' inferred. In C or similar, when you delcair the variable you are
setting aside the memory to hold something of that type. This is compile
time typing. That isn't how python works, so naming something an int will
never make it an int. intMe = 'Joel' is totally valid in python.

Sticking to ideas like this will hinder understanding of how python works.
I suggest taking two hours to study the python documentation at python.org.
I don't speak Chinese, but I know that I can't just use a dictionary of
English to Chinese and use the same syntax. It won't be Chinese.

Get over your prejudice and learn the new language.... or don't, but trying
to shoe horn python into the concepts of another language won't help you
understand python, it will produce ugly, messy, unsupportable code.

>
> In practice, I wouldn't define an intX_asString var, I would do "str
> (num)" every time a string representation is needed, provided it isn't a
> loop, as in that context the expression would probably negatively impact
> performance in an interpreted language.
>
> Peter
> --
> http://mail.python.org/mailman/listinfo/python-list
>




--
Joel Goldstick
http://joelgoldstick.com

 
Reply With Quote
 
Joshua Landau
Guest
Posts: n/a
 
      02-24-2013
On 24 February 2013 22:43, <(E-Mail Removed)> wrote:

> Josh,
>
> Not thank you for your malicious post.
>


Be careful, us programmers do *eventually* catch on to who is a troll, and
if you say things like that we may eventually mark you off as just to
hostile.
I *honestly* meant no malice or insult. If you can't take my word, you can
point out what I said that was otherwise.

(Then again, you'll have about a week before we really start to notice )


> I think you are missing the point here.
>
> My source code was just a dummy to offer context for the question I wanted
> to ask. Further down the line, if I ever feel I don't need to
> pseudo-declare variables I will stop doing it. But for the moment I am
> trying to imitate familiar ground.
>
> My code as written has no syntax errors, so what's the problem? It is
> highly unlikely you will ever read any of my Python code - no need to get
> excited over a few of my lines.
>


You said "Any comments on this before I quit my job?".

I commented on how I think you should approach Python in order to
appreciate its virtues rather than get stuck in its differences. Again, I
am no good programmer, but I think these methods will help you.


> And you don't need to answer questions which were not posed, thank you.
>


Nor do I need to answer questions which were posed.


> I wanted Python to register what type of variable I'm after. So I init my
> vars accordingly, int might be 0, float 0.0 and string with null, err...
> None.
>


You seem to think that a "null" version of a type is the falsy version.
Then:
int -> 0
float -> 0.
tuple -> ()
list -> []

And then (*dun dun duuun!*):

str -> "" (NOT None, which is a different type)

Other people have commented on whether this is a good idea (it's not), so
I'll suggest you read those, too.

In practice, I wouldn't define an intX_asString var, I would do "str (num)"
> every time a string representation is needed, provided it isn't a loop, as
> in that context the expression would probably negatively impact performance
> in an interpreted language.



PS: Guess what str(None) is.

 
Reply With Quote
 
Oscar Benjamin
Guest
Posts: n/a
 
      02-24-2013
On 24 February 2013 21:35, Joshua Landau <(E-Mail Removed)> wrote:
>
> determinant = b**2 - 4*a*c


It's called the discriminant. A determinant is something altogether different.


Oscar
 
Reply With Quote
 
Albert Hopkins
Guest
Posts: n/a
 
      02-24-2013


> Most of what gets hung in art galleries these days is far less
> visually pleasing than well-written code.


+1 QOTW
 
Reply With Quote
 
piterrr.dolinski@gmail.com
Guest
Posts: n/a
 
      02-24-2013

>> intX = 32 # decl + init int var

> How is it not obvious that "intX" is an integer *without* the comment?


Indeed the assignment is enough to deduce "intX" is an int. The comment is there to let me know it is unlikely intX appears earlier in the code. Please, let me do things my way until I find reasons to the contrary.

Regarding my use of None to mean NULL, point taken to use "" instead.

Peter
 
Reply With Quote
 
piterrr.dolinski@gmail.com
Guest
Posts: n/a
 
      02-24-2013

>> intX = 32 # decl + init int var

> How is it not obvious that "intX" is an integer *without* the comment?


Indeed the assignment is enough to deduce "intX" is an int. The comment is there to let me know it is unlikely intX appears earlier in the code. Please, let me do things my way until I find reasons to the contrary.

Regarding my use of None to mean NULL, point taken to use "" instead.

Peter
 
Reply With Quote
 
Chris Angelico
Guest
Posts: n/a
 
      02-24-2013
On Mon, Feb 25, 2013 at 9:33 AM, Ethan Furman <(E-Mail Removed)> wrote:
> On 02/24/2013 12:58 PM, Chris Angelico wrote:
>>
>> On Mon, Feb 25, 2013 at 7:34 AM, Ethan Furman <(E-Mail Removed)> wrote:
>>>
>>>
>>> - no variable declarations, just use 'em

>>
>>
>> Variable declarations can go either way; Python requires you to name
>> all globals that you mutate

>
>
> I'm not sure what you mean -- example?


Whoops, said the wrong thing. All globals that you assign to.

>>> a=1
>>> b=[]
>>> def foo(x):

y=x+1
global a
a+=x
b.append(y)
>>> foo(2)
>>> a

3
>>> b

[3]

Python requires that you name 'a' in a global statement; C would
require a declaration for 'y' to make it local. PHP, meanwhile, would
require declarations for both a and b.

ChrisA
 
Reply With Quote
 
Chris Angelico
Guest
Posts: n/a
 
      02-24-2013
On Mon, Feb 25, 2013 at 10:38 AM, <(E-Mail Removed)> wrote:
>
>>> intX = 32 # decl + init int var

>> How is it not obvious that "intX" is an integer *without* the comment?

>
> Indeed the assignment is enough to deduce "intX" is an int. The comment is there to let me know it is unlikely intX appears earlier in the code. Please, let me do things my way until I find reasons to the contrary.
>
> Regarding my use of None to mean NULL, point taken to use "" instead.


It's worth noting that None does make a good rendition of
"null-as-opposed-to-blank", for instance when you're fetching data
from an VARCHAR field in an SQL database. You'd use a string for
anything that isn't NULL, and None for the others.

ChrisA
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(Python newbie) Using XP-SP2/MSVC6: No Python24_d.lib, winzip barfs on Python-2.4.1.tar, cannot download bzip2 Bill Davy Python 12 04-22-2005 12:19 PM
Re: (Python newbie) Using XP-SP2/MSVC6: No Python24_d.lib,winzip barfs on Python-2.4.1.tar, cannot download bzip2 Jaime Wyant Python 0 04-21-2005 12:49 PM
RE: (Python newbie) Using XP-SP2/MSVC6: No Python24_d.lib,winzip barfs on Python-2.4.1.tar, cannot download bzip2 Bill Davy Python 0 04-19-2005 09:27 AM
Python Newbie. Python on PPC, please assist. pythonnewbie Python 5 10-12-2004 05:51 PM
Creating an OLE server document in Python (MFC/OLE/COM/Python newbie) Drew Pihera Python 0 02-04-2004 07:48 PM



Advertisments