A dumb question about a class


I'm still trying to understand classes. I've made some progress, I
think, but I don't understand how to use this one. How do I call it,
or any of its functions It's from the Cookbook, at
.

Posted On: Wednesday 7th of November 2012 01:35:12 PM Total Views:  402
View Complete with Replies

RELATED TOPICS OF Python Programming PROGRAMMING LANGUAGE




Re: Another dumb scope question for a closure.

On Wed, 9 Jan 2008 13:47:30 -0500 (EST) "Steven W. Orr" wrote: > So sorry because I know I'm doing something wrong. > > 574 > cat c2.py > #! /usr/local/bin/python2.4 > > def inc(jj): > def dummy(): > jj = jj + 1 > return jj > return dummy > > h = inc(33) > print 'h() = ', h() > 575 > c2.py > h() = > Traceback (most recent call last): > File "./c2.py", line 10, in > print 'h() = ', h() > File "./c2.py", line 5, in dummy > jj = jj + 1 > UnboundLocalError: local variable 'jj' referenced before assignment > > I could have sworn I was allowed to do this. How do I fix it Nope. This is one of the things that makes lisper's complain that Python doesn't have "real closures": you can't rebind names outside your own scope (except via global, which won't work here). Using a class is the canonical way to hold state. However, any of the standard hacks for working around binding issues work. For instance: >>> def inc(jj): .... def dummy(): .... box[0] = box[0] + 1 .... return box[0] .... box = [jj] .... return dummy .... >>> h = inc(33) >>> h() 34
VIEWS ON THIS POST

152

Posted on:

Monday 5th November 2012
View Replies!

Re: Another dumb scope question for a closure.

On Wed, 9 Jan 2008 13:47:30 -0500 (EST) "Steven W. Orr" wrote: > So sorry because I know I'm doing something wrong. > > 574 > cat c2.py > #! /usr/local/bin/python2.4 > > def inc(jj): > def dummy(): > jj = jj + 1 > return jj > return dummy > > h = inc(33) > print 'h() = ', h() > 575 > c2.py > h() = > Traceback (most recent call last): > File "./c2.py", line 10, in > print 'h() = ', h() > File "./c2.py", line 5, in dummy > jj = jj + 1 > UnboundLocalError: local variable 'jj' referenced before assignment > > I could have sworn I was allowed to do this. How do I fix it Nope. This is one of the things that makes lisper's complain that Python doesn't have "real closures": you can't rebind names outside your own scope (except via global, which won't work here). Using a class is the canonical way to hold state. However, any of the standard hacks for working around binding issues work. For instance: >>> def inc(jj): .... def dummy(): .... box[0] = box[0] + 1 .... return box[0] .... box = [jj] .... return dummy .... >>> h = inc(33) >>> h() 34
VIEWS ON THIS POST

192

Posted on:

Monday 5th November 2012
View Replies!

Another dumb scope question for a closure.

So sorry because I know I'm doing something wrong. 574 > cat c2.py #! /usr/local/bin/python2.4 def inc(jj): def dummy(): jj = jj + 1 return jj return dummy h = inc(33) print 'h() = ', h() 575 > c2.py h() = Traceback (most recent call last): File "./c2.py", line 10, in print 'h() = ', h() File "./c2.py", line 5, in dummy jj = jj + 1 UnboundLocalError: local variable 'jj' referenced before assignment I could have sworn I was allowed to do this. How do I fix it -- Time flies like the wind. Fruit flies like a banana. Stranger things have .0. happened but none stranger than this. Does your driver's license say Organ ..0 DonorBlack holes are where God divided by zero. Listen to me! We are all- 000 individuals! What if this weren't a hypothetical question steveo at syslang.net
VIEWS ON THIS POST

133

Posted on:

Monday 5th November 2012
View Replies!

Re: Another dumb scope question for a closure.

Steven W. Orr wrote: > So sorry because I know I'm doing something wrong. > > 574 > cat c2.py > #! /usr/local/bin/python2.4 > > def inc(jj): > def dummy(): > jj = jj + 1 > return jj > return dummy > > h = inc(33) > print 'h() = ', h() > 575 > c2.py > h() = > Traceback (most recent call last): > File "./c2.py", line 10, in > print 'h() = ', h() > File "./c2.py", line 5, in dummy > jj = jj + 1 > UnboundLocalError: local variable 'jj' referenced before assignment http://docs.python.org/ref/naming.html has the answer: "If a name binding operation occurs anywhere within a code block, all uses of the name within the block are treated as references to the current block." > I could have sworn I was allowed to do this. How do I fix it use a class to hold state, like else does
VIEWS ON THIS POST

142

Posted on:

Monday 5th November 2012
View Replies!

Re: Another dumb scope question for a closure.

On Wednesday, Jan 9th 2008 at 14:01 -0000, quoth Fredrik Lundh: =>Steven W. Orr wrote: => =>> So sorry because I know I'm doing something wrong. =>> =>> 574 > cat c2.py =>> #! /usr/local/bin/python2.4 =>> =>> def inc(jj): =>> def dummy(): =>> jj = jj + 1 =>> return jj =>> return dummy =>> =>> h = inc(33) =>> print 'h() = ', h() =>> 575 > c2.py =>> h() = =>> Traceback (most recent call last): =>> File "./c2.py", line 10, in =>> print 'h() = ', h() =>> File "./c2.py", line 5, in dummy =>> jj = jj + 1 =>> UnboundLocalError: local variable 'jj' referenced before assignment => =>http://docs.python.org/ref/naming.html has the answer: => => "If a name binding operation occurs anywhere within a code block, => all uses of the name within the block are treated as references => to the current block."
VIEWS ON THIS POST

343

Posted on:

Wednesday 7th November 2012
View Replies!

Re: Library package import question

2007/11/5, Frank Aune : > To prevent namespace pollution, I want to import and use this library in the > following way: > > import Foo > (...) > t = Foo.module2.Bee() from x import y as z that has always worked for me to prevent pollution... -- http://noneisyours.marcher.name http://feeds.feedburner.com/NoneIsYours
VIEWS ON THIS POST

138

Posted on:

Wednesday 7th November 2012
View Replies!

simple? embedding question

suppose i have imported two modules foo and bar with foo=PyImport_ImportModule("foo") and bar=PyImport_ImportModule("bar") respectively. Now suppose I have an artitrary python expression to evaluate. Do I need to parse that thring and check for foo. and bar. before jumping the usual PyModule_GetDict,PyDict_GetItemString,PyObject_CallObject hoop hoop on the PyObject for the prefix or there is a better way btw PyRun_SimpleString("foo.baz()"); does not work: Traceback (most recent call last): File "", line 1, in NameError: name 'foo' is not defined and i potentially need a PyObject* back with the result of the expression anyway.
VIEWS ON THIS POST

154

Posted on:

Wednesday 7th November 2012
View Replies!

Fwd: Namespace question

2007/10/31, Frank Aune : > , > > Is it possible writing custom modules named the same as modules in the > standard library, which in turn use the same module from the standard > library > > Say I want my application to have a random.py module, which in turn must > import the standard library random.py module also, to get hold of the randint > function for example. > > My attempts so far only causes my random.py to import itself instead of the > standard library random.py > > Receipt for disaster You mean something like this: >>>import random >>>def foo(): .... print '42' >>>random.randit = foo >>>random.randit() 42 am I right -- Wbr, Andrii Mishkovskyi. He's got a heart of a little child, and he keeps it in a jar on his desk.
VIEWS ON THIS POST

101

Posted on:

Wednesday 7th November 2012
View Replies!

Simple question about python logic.

I have a file containing following data. But the dimension can be different. A B C D E F G 3 4 1 5 6 2 4 7 2 4 1 6 9 3 3 4 1 5 6 2 4 7 2 4 1 6 9 3 .. .. .. .. What is the best approach to make a column vector with the name such as A B, etc
VIEWS ON THIS POST

135

Posted on:

Wednesday 7th November 2012
View Replies!

Re: for loop question

On Wed, 2007-10-10 at 14:56 -0500, Robert Dailey wrote: > > > I'm currently writing my own CSV parser since the built in one doesn't > support Unicode. Why do you think you need a CSV parser that supports unicode -- Carsten Haese http://informixdb.sourceforge.net
VIEWS ON THIS POST

135

Posted on:

Wednesday 7th November 2012
View Replies!

a question about decorator

Hi: please look at this code. def A(): print 'warp in A' def why(self, *arg, **kw): print 'in A' print self print arg print kw #self(*arg, **kw) return why class T(object): @A() def test(g, out): print 'in test', out it will out put: warp in A in A () {} the function why will be called, why there is no code to call it. Kyo.
VIEWS ON THIS POST

230

Posted on:

Wednesday 7th November 2012
View Replies!

some questions about Python and tkinter

Hi every one, I m a sysadmin who want to know how to use python. I dont know anything about oriented object programation, i only know bash and a little perl. I have some simple questions about python. the first: what is the differences between a function and a classe In which case i should use a function In which case i should use a class The second: there is some pincipals gui toolkit: tkinter , Python Mega- Widgets, PyGTK, PyQt, FxPy, WxPy what are the advantages of each one, and in which case i use each of them
VIEWS ON THIS POST

168

Posted on:

Wednesday 7th November 2012
View Replies!

Re: re question

On 9/19/07, Dan Bar Dov wrote: > I'm trying to construct a regular expression to match valid IP address, > without leading zeroes (i.e > 1.2.3.4, 254.10.0.0, but not 324.1.1.1, nor 010.10.10.1) > > This is what I come up with, and it does not work. > > r'(^[12]\d{0,2}\.){3,3}[12]\d{0,2}' > > What am I doing wrong I'm not sure what affect having the "^" inside of the parens will have, but it surely isn't what you want. This part: r"[12]\d{0,2}" will match the following strings, which I'm sure you dont' want: "" - yes it will match an empty string (Is "..." a valid IP) "00" - It could start with a 0, as long as there are only two characters "299" - A little outside of the range you are interested in That {3,3} is better written as {3}. > Any common knowledge IP matching RE I don't know if there is any common knowledge RE, but I came up with the following: r"((1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|\d)\.){3}(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|\d)") Let us break it down: This matches an octet: r"(1\d{2}|2[0-5]\d|[1-9]\d|\d)" Which will match any ONE of the following 1\d{2} - A "1" followed by any two digits 2[0-4]\d - A "2" followed by 0,1,2,3 or 4 followed by any digit 25[0-5] - A "25" followed by 0,1,2,3,4 or 5 [1-9]\d - Any digit but 0 followed by any digit \d - Any Digit I generally discourage people from using REs. I think the folowing is much easier to read: def isip(x): octs = x.split(".") if len(octs) != 4: return False for oct in octs: if len(oct) > 1 and oct[0] == "0": return False try: if not 0
VIEWS ON THIS POST

132

Posted on:

Wednesday 7th November 2012
View Replies!

Re: re question

Dan Bar Dov wrote: > I'm trying to construct a regular expression to match valid IP address, > without leading zeroes (i.e > 1.2.3.4 , 254.10.0.0 , but not > 324.1.1.1, nor 010.10.10.1 ) > > This is what I come up with, and it does not work. > > r'(^[12]\d{0,2}\.){3,3}[12]\d{0,2}' > > What am I doing wrong > Any common knowledge IP matching RE > >
VIEWS ON THIS POST

194

Posted on:

Wednesday 7th November 2012
View Replies!

question on python syntax

If I type this in shell $ ./yourfile.py 12:34 PM & What does '$', '.', '/' and '& means in this succession Note: 12:34 PM is a argument to the yourfile.py.
VIEWS ON THIS POST

159

Posted on:

Wednesday 7th November 2012
View Replies!

Newbie question

I have exp with C/C++ (and a few other langs). I want to use Python to start doing the ff: 1). Data Munging (text processing) 2). Automating my build process 3). (Possibly) some web data retrieval jobs Can anyone point me to resurces/possibly scripts that can get me up to speed (to do these 3 things) ASAP, without having to learn the basics of programming
VIEWS ON THIS POST

105

Posted on:

Wednesday 7th November 2012
View Replies!

Re: RegEx question

On 15:25 Thu 04 Oct , Robert Dailey wrote: > I am not a regex expert, I simply assumed regex was standardized to follow > specific guidelines. There are as many different regex flavours as there are Linux distros. Each follows the basic rules but implements them slightly differently and adds their own 'extensions'. > I also made the assumption that this was a good place > to pose the question since regular expressions are a feature of Python. The best place to pose a regex question is in the sphere of usage, i.e. Perl regexes differ hugely in implementation from OO langs like Python or Java, while shells like bash or zsh use regexes slightly differently, as do shell scripting languages like awk or sed. > The question concerned regular expressions in general, not really the > application. However, now that I know that regex can be different, I'll try > to contact the author directly to find out the dialect and then find the > appropriate location for my question from there. I do appreciate 's > help. I've tried the various suggestions offered here, however none of them > work. I can only assume at this point that this regex is drastically > different or the application reading the regex is just broken. If you care to PM me with details of the language/context I will try to help but I am no expert.
VIEWS ON THIS POST

115

Posted on:

Wednesday 7th November 2012
View Replies!

subprocess.Popen(cmd) question

, I'm starting some subprocesses inside a loop. The processes run independent and dont need any communication between each other. Due to memory issues I need to limit the number of running processes to around 10. How can I insert a break into my loop to wait until some processes are finished Some minimal examplecode: import subprocess for i in range(0,100): cmd='ping localhost' p=subprocess.Popen(cmd) p.wait()
VIEWS ON THIS POST

147

Posted on:

Wednesday 7th November 2012
View Replies!

string questions

Hi , I'm a beginning programming student in Python and have a few questions regarding strings. If s1 = "spam" If s2 = "ni!" 1. Would string.ljust(string.upper(s2),4) * 3 start it at the left margin and move it 12 spaces to the right because of the 4 *3 If so, why is it in the parathesis for the upper command and not the ljust I already know that it would cap it to NI! 2. To get the output "Spam Ni! Spam Ni! Spam Ni!" I could do something like this string.join ([s1, s2]), But I'm a little lost how to get it repeated three times on one line. Would I just have to put the same command on the next two lines 3. To change spam to spm, the string.replace seems to be the best function to use. However, when I use string.replace(s1, "a", " ") in python to replace a with an empty space, it doesn't work...I just get spam back when I print s1. Any ideas
VIEWS ON THIS POST

111

Posted on:

Wednesday 7th November 2012
View Replies!

Re: Import question

Lamonte Harris wrote: > can I import more then one modules like this: > > import module,module2 So your'e basically saying that you haven't tried it /W
VIEWS ON THIS POST

187

Posted on:

Wednesday 7th November 2012
View Replies!