Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #78750 > unrolled thread
| Started by | roro codeath <rorocodeath@gmail.com> |
|---|---|
| First post | 2014-10-06 18:06 +0800 |
| Last post | 2014-10-08 03:12 -0700 |
| Articles | 7 — 4 participants |
Back to article view | Back to comp.lang.python
ruby instance variable in python roro codeath <rorocodeath@gmail.com> - 2014-10-06 18:06 +0800
Re: ruby instance variable in python Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-10-07 05:54 +1100
Re: ruby instance variable in python flebber <flebber.crue@gmail.com> - 2014-10-07 05:29 -0700
Re: ruby instance variable in python Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2014-10-07 15:46 +0300
Re: ruby instance variable in python flebber <flebber.crue@gmail.com> - 2014-10-07 17:59 -0700
Re: ruby instance variable in python Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-10-08 12:32 +1100
Re: ruby instance variable in python flebber <flebber.crue@gmail.com> - 2014-10-08 03:12 -0700
| From | roro codeath <rorocodeath@gmail.com> |
|---|---|
| Date | 2014-10-06 18:06 +0800 |
| Subject | ruby instance variable in python |
| Message-ID | <mailman.14632.1412590002.18130.python-list@python.org> |
[Multipart message — attachments visible in raw view] — view raw
in ruby:
module M
def ins_var
@ins_var ||= nil
end
def m
@ins_var = 'val'
end
def m2
m
ins_var # => 'val'
end
end
in py:
# m.py
# how to def ins_var
def m:
# how to set ins_var
def m2:
m()
# how to get ins var
[toc] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2014-10-07 05:54 +1100 |
| Message-ID | <5432e55b$0$12995$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #78750 |
roro codeath wrote:
> in ruby:
>
> module M
> def ins_var
> @ins_var ||= nil
> end
>
> def m
> @ins_var = 'val'
> end
>
> def m2
> m
> ins_var # => 'val'
> end
> end
I admit that my Ruby skills are admittedly pretty lousy. Still, I used to
think that Ruby was pretty readable, but I find the above completely
meaningless. So I'm going to guess what you want, sorry if I guess wrong.
> in py:
>
> # m.py
>
> # how to def ins_var
>
> def m:
> # how to set ins_var
>
> def m2:
> m()
> # how to get ins var
Please explain what you mean by "instance variable". There are two standard
things which it could be.
(1) A string variable is a variable holding a string. A float variable is a
variable holding a float. A list variable is a variable holding a list. So
an instance variable must be a variable holding an instance.
ins_var = NameOfTheClass(arg)
You just instantiate the class, passing whatever arguments it expects.
(2) In the Java world, "instance variable" doesn't mean a variable at all,
but an attribute of classes which is stored on the instance. (As opposed to
those attributes stored on the class itself, which they refer to
as "static", since in Java they are known to the compiler.)
To define "instance variables" (attributes), you have to have a class to
define them in. Remember that Python uses the "Offside Rule" (significant
indentation).
class MyClass(object): # subclass of object
def __init__(self, arg):
# Initialise the instance.
self.ins_var = arg
def method(self, arg):
return self.ins_var == arg:
instance = MyClass("some value")
print(instance.ins_var) # prints "some value"
instance.method("spam and eggs") # returns False
instance.method("some value") # returns True
Does this help?
--
Steven
[toc] | [prev] | [next] | [standalone]
| From | flebber <flebber.crue@gmail.com> |
|---|---|
| Date | 2014-10-07 05:29 -0700 |
| Message-ID | <b22416b5-e3ed-4ece-aeba-65603fff1b3f@googlegroups.com> |
| In reply to | #78750 |
On Monday, 6 October 2014 21:07:24 UTC+11, roro codeath wrote:
> in ruby:
>
>
> module M
> def ins_var
> @ins_var ||= nil
> end
>
>
> def m
> @ins_var = 'val'
> end
>
>
> def m2
> m
> ins_var # => 'val'
> end
> end
>
>
> in py:
>
>
> # m.py
>
>
> # how to def ins_var
>
>
> def m:
> # how to set ins_var
>
>
> def m2:
> m()
> # how to get ins var
I took || to be a ternary. So I assumed your code just sets ins_var to nil and then is called in module m and supplied a val. Could be wrong.
if ins_var is None:
ins_var = 'val'
[toc] | [prev] | [next] | [standalone]
| From | Jussi Piitulainen <jpiitula@ling.helsinki.fi> |
|---|---|
| Date | 2014-10-07 15:46 +0300 |
| Message-ID | <qotzjd8nj3d.fsf@ruuvi.it.helsinki.fi> |
| In reply to | #78797 |
flebber writes: > On Monday, 6 October 2014 21:07:24 UTC+11, roro codeath wrote: > > in ruby: > > > > > > module M > > def ins_var > > @ins_var ||= nil > > end ... > I took || to be a ternary. So I assumed your code just sets ins_var > to nil and then is called in module m and supplied a val. Could be > wrong. > > if ins_var is None: > ins_var = 'val' Just out of interest, please, do you think the word 'ternary' is more or less synonymous with 'conditional'? I'm not being sarcastic. This possibility just occurred to me, and the world will begin to make more sense to me if it turns out that there are people who simply do not think 'three' when they think 'ternary'.
[toc] | [prev] | [next] | [standalone]
| From | flebber <flebber.crue@gmail.com> |
|---|---|
| Date | 2014-10-07 17:59 -0700 |
| Message-ID | <48972e63-c980-4025-a624-cda0b60367de@googlegroups.com> |
| In reply to | #78799 |
I thought that it was a shortcut in ruby to negate the other option of providing another default . I don't greatly know ruby but took a guess after reading examples here https://blog.neowork.com/ruby-shortcuts
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2014-10-08 12:32 +1100 |
| Message-ID | <54349433$0$13001$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #78818 |
flebber wrote: > I thought that it was a shortcut in ruby to negate the other option of > providing another default . I'm afraid I can't work out what that sentence means, "to negate the other option of providing *another* default"? How many defaults are you providing? Then you negate *the option*, does that mean that you're not actually providing a default? Also, since you haven't quoted the person you are responding to, there is no context to your response. The end result of a confusing sentence with no context is that I have no idea what you are trying to say. Could you try explaining again please? -- Steven
[toc] | [prev] | [next] | [standalone]
| From | flebber <flebber.crue@gmail.com> |
|---|---|
| Date | 2014-10-08 03:12 -0700 |
| Message-ID | <54c71633-8657-460e-9bf3-d20350e77a2e@googlegroups.com> |
| In reply to | #78819 |
The end result of a confusing sentence with no
>
> context is that I have no idea what you are trying to say. Could you try
>
> explaining again please?
>
>
> Steven
No problem my reply from phone at work a little confusing.
So trying to determine what this does.
def ins_var
@ins_var ||= nil
end
In particular I was guessing at this.
@ins_var ||= nil
Which I have now found on Rubyinside http://www.rubyinside.com/21-ruby-tricks-902.html
From there
7 - Cut down on local variable definitions
Instead of defining a local variable with some initial content (often just an empty hash or array), you can instead define it "on the go" so you can perform operations on it at the same time:
(z ||= []) << 'test'
2009 Update: This is pretty rancid, to be honest. I've changed my mind; you shouldn't be doing this :)
So now that I know this I am still further lost to the point of the initially posted code so my kubuntu has ruby so I have run it, and honestly I need further definition on what that code was trying to acheive.
sayth@sayth-TravelMate-5740G:~/scripts$ ruby --version
ruby 1.9.3p484 (2013-11-22 revision 43786) [x86_64-linux]
sayth@sayth-TravelMate-5740G:~/scripts$ irb
irb(main):001:0> (z ||= []) << 'test'
=> ["test"]
irb(main):002:0> @ins_var ||= nil
=> nil
irb(main):003:0> def ins_var
irb(main):004:1> @ins_var ||= nil
irb(main):005:1> end
=> nil
irb(main):006:0> def m
irb(main):007:1> @ins_var = "val"
irb(main):008:1> end
=> nil
irb(main):009:0> def m2
irb(main):010:1> ins_var #=> "val"
irb(main):011:1> end
=> nil
irb(main):012:0> m
=> "val"
irb(main):013:0> m2
=> "val"
Sayth
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web