Ive spent a few days going thru a couple of Python tutorials. No
problem until I got to classes. I guess my java mindset is blocking my
vision. I've borrowed another thread's code snippet and cannot explain
the results:
class MyClass:
list = []
myvar = 10
def add(self, x):
self.list.appen d(x)
self.myvar = x
def printer(self):
print self.list
print self.myvar
a = MyClass()
b = MyClass()
for n in range(20):
a.add(n)
print "list in a:"
a.printer()
print "list in b:"
b.printer()
This produces:
list in a:
list in a:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
19
list in b:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
10
WHY do the "class members" list and myvar seem to behave differently?
Given the way that list[] is supposedly shared why doesnt myvar exhibit
the same behavior? It seems that myvar is acting like a true "instance
member". Is this simply because of the underlying types?
TIA 2 1674
hdixon a écrit :
Ive spent a few days going thru a couple of Python tutorials. No
problem until I got to classes. I guess my java mindset is blocking my
vision.
Then http://dirtsimple.org/2004/12/python-is-not-java.html
I've borrowed another thread's code snippet and cannot explain
the results:
class MyClass:
class MyClass(object) :
list = []
This may not be too harmful in the given context, nut shadowing builtin
types may not be a good idea.
myvar = 10
def add(self, x):
self.list.appen d(x)
"list" is a class attribute. It's shared by all instances of MyClass.
This is a FAQ AFAICT.
self.myvar = x
This creates an instance attribute named myvar that shadows the class
attribute of the same name
(snip expected results)
WHY do the "class members" list and myvar seem to behave differently?
cf above. And notice that in Python, binding (aka 'assignement') and
name lookup on objects are different beasts. There have been quite a few
threads about this recently.
Given the way that list[] is supposedly shared why doesnt myvar exhibit
the same behavior?
Because binding to self.myvar creates the instance attribute "myvar".
Notice that you have not rebound "list".
It seems that myvar is acting like a true "instance
member".
It is one.
Is this simply because of the underlying types?
Nope, it's because in the first case (MyClass.list, accessed as
self.list) you just call methods on the list object, which has no effect
on the binding.
Bruno Desthuilliers wrote:
hdixon a écrit :
Ive spent a few days going thru a couple of Python tutorials. No
problem until I got to classes. I guess my java mindset is blocking my
vision.
Then http://dirtsimple.org/2004/12/python-is-not-java.html
I've borrowed another thread's code snippet and cannot explain
the results:
class MyClass:
class MyClass(object) :
list = []
This may not be too harmful in the given context, nut shadowing builtin
types may not be a good idea.
myvar = 10
def add(self, x):
self.list.appen d(x)
"list" is a class attribute. It's shared by all instances of MyClass.
This is a FAQ AFAICT.
self.myvar = x
This creates an instance attribute named myvar that shadows the class
attribute of the same name
(snip expected results)
WHY do the "class members" list and myvar seem to behave differently?
cf above. And notice that in Python, binding (aka 'assignement') and
name lookup on objects are different beasts. There have been quite a few
threads about this recently.
Given the way that list[] is supposedly shared why doesnt myvar exhibit
the same behavior?
Because binding to self.myvar creates the instance attribute "myvar".
Notice that you have not rebound "list".
It seems that myvar is acting like a true "instance
member".
It is one.
Is this simply because of the underlying types?
Nope, it's because in the first case (MyClass.list, accessed as
self.list) you just call methods on the list object, which has no effect
on the binding.
ok - i "think" its beginning to dawn on me. BTW - the blog above was a
good read for me. I _am_ trying to bring java into the mix and its
screwing me up big time. I'll go back over the chapters that dicuss
binding/name lookups.
Thanks This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Neil Zanella |
last post by:
Hello,
I would like to know whether it is possible to define static class methods
and data members in Python (similar to the way it can be done in C++ or Java).
These do not seem to be mentioned in "Learning Python" by Mark Lutz and David
Ascher. It seems like they are a relatively new feature... It seems to me that
any truly OO programming language should support these so I'm sure that Python
is no exception, but how can these be...
|
by: kuvpatel |
last post by:
Hi
I want to refer a class called LogEvent, and use one of its methods called
WriteMessage without actually having to create an instance of Logevent.
I have tried using the word sealed with the class and this works but I would
also like to know of other ways to do this.
Also are there any performance implacations of using sealed?
|
by: Chris |
last post by:
Hi,
I don't get the difference between a struct and a class !
ok, I know that a struct is a value type, the other a reference type, I
understand the technical differences between both, but conceptually speaking
: when do I define something as 'struct' and when as 'class' ?
for example : if I want to represent a 'Time' thing, containing :
- data members : hours, mins, secs
|
by: thomson |
last post by:
Hi all,
Would you please explain me where will be the heap stored if it
is declared inside the Class, As class is a reference type, so it gets
stored on the heap, but struct is a value type-stored on the stack
Regards
thomson
|
by: kevin |
last post by:
I have a form and in the form I have a sub that uses a class I instantiate
using
visual basic code: Public oCP As New Rs232 'instantiate the comm port
I need to share this sub with another form so to declare the sub I use
visual basic code: Public Shared Function IsPortAvailable(ByVal ComPort
As Integer) As Boolean
| |
by: Jamey Shuemaker |
last post by:
I'm in the process of expanding my knowledge and use of Class Modules.
I've perused MSDN and this and other sites, and I'm pretty comfortable
with my understanding of Class Modules with the exception of custom
Collection Classes.
Background: I'm developing an A2K .mdb to be deployed as an .mde at my
current job-site. It has several custom controls which utilize custom
classes to wrap built-in controls, and add additional functionality....
|
by: learning |
last post by:
Hi I have a static class written by other team which encapsulates a
database instance. but I need to extend it to incldue other things. I
know that C# static class is sealed and can;t be inherited & I don't
want to copy + paste code. How can I inherit those member variables?
Thanks
|
by: jan.loucka |
last post by:
Hi,
We're building a mapping application and inside we're using open
source dll called MapServer. This dll uses object model that has quite
a few classes. In our app we however need to little bit modify come of
the classes so they match our purpose better - mostly add a few
methods etc.
Example: Open source lib has classes Map and Layer defined. The
relationship between them is one to many.
We created our own versions (inherited) of Map...
|
by: =?Utf-8?B?R2Vvcmdl?= |
last post by:
Hello everyone,
I met with a strange issue that derived class function can not access base
class's protected member. Do you know why?
Here is the error message and code.
error C2248: 'base::~base' : cannot access protected member declared in
|
by: puzzlecracker |
last post by:
Would you quickly remind me the difference between, regular class,
static class, and nested class?
Thanks
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |